vendredi 9 décembre 2011

Google Maps API2: How to find latitude and longitude (geocode) for any well formatted address.

Recently I was working on a project to develop a city guide that helps tourists live locally for any specified city in the world. One interesting feature was to propose to the user sites closer to his home based address or current location. The idea is to form a circle with a user specified radius, with center his current location or home based address. Getting the center of this circle involves getting the latitude and longitude (geocode) of the current location. The following javascript function uses the GClientGeocoder() of Google Maps API to find out accurate geographical coordinates (latitude and longitude) for any place on Earth:

1:  function getLatLng(address) {  
2:       //alert('getting the center of the circle');  
3:       if (GBrowserIsCompatible()) {  
4:             var geocoder = new GClientGeocoder();  
5:               
6:             geocoder.getLatLng(address, function(latlng) {  
7:              if (!latlng) {  
8:                  alert('"' + address + '" not found');  
9:                  return false;  
10:              } else {  
11:                  return {'Lat':latlng.lat().toFixed(6), 'Lng':latlng.lng().toFixed(6)};               
13:              }  
14:             });  
15:       }  
16:   }  

This works only for compartible browsers. You can check browser compatibility using GBrowserIsCompatible() and of course you need to include the API using
1:  <script src="http://maps.google.com/maps?file=api&v=2&key=abcdef" type="text/javascript"></script>  

dimanche 21 août 2011

Google Maps API: Getting the distance, driving duration and driving directions between two adresses

Sometimes you may want to get the distance between addresses and maybe suggest driving directions. The Google Maps API Web Services makes this possible with the DirectionService and DistanceMatrixService classes. Below is a JavaScript undocumented snippet that does the task and more details about the fore mentioned services can be found in the above link. A walk through of the code would freely be given upon request.


1:  <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>   
2:    <script type="text/javascript">   
3:       
4:     var directionsService = new google.maps.DirectionsService();  
5:           
6:       
7:     var origin1 = "Nice, France";   
8:     var origin2 = "Buea, Cameroon";  
9:    
10:    
11:     var destination = "Montpellier, France";  
12:     var destination2 = "Douala, Cameroon";  
13:       
14:     function calculateDistances() {  
15:      var service = new google.maps.DistanceMatrixService();  
16:      service.getDistanceMatrix(  
17:       {  
18:        origins: [origin1, origin2],  
19:        destinations: [destination1, destination2],  
20:        travelMode: google.maps.TravelMode.DRIVING,  
21:        unitSystem: google.maps.UnitSystem.METRIC,  
22:        avoidHighways: false,  
23:        avoidTolls: false  
24:       }, callback);  
25:     }  
26:    
27:     function callback(response, status) {  
28:          
29:      if (status != google.maps.DistanceMatrixStatus.OK) {  
30:       alert('Error was: ' + status);  
31:      } else {  
32:              
33:       var origins = response.originAddresses;  
34:       var destinations = response.destinationAddresses;  
35:       var outputDiv = $('#outputDiv'); //div to hold distance and driving time  
36:       outputDiv.html('');  
37:         
38:       var html = "<b> <br /> Ground Estimates <br /><br /> </b>";  
39:               
40:       for (var i = 0; i < origins.length; i++) {  
41:       var results = response.rows[i].elements;  
42:          
43:        for (var j = 0; j < results.length; j++) {  
44:           
45:                  html += " Path : "+ origins[i] + " to " + destinations[j] + "<br />";  
46:                  html +="<b> Approx. Distance </b>: "+ results[j].distance.text + "<br />";  
47:                  html +="<b> Approx. Duration </b>: "+ results[j].duration.text + "<br /><br />";  
48:                         
49:                  getDrivingDirections(origins[i], destinations[j]);  
50:                    
51:        }  
52:       }  
53:              $('#outputDiv').html(html);  
54:                
55:      }  
56:     }  
57:          
58:     function getDrivingDirections(start, end) {  
59:        
60:      var request = {  
61:       origin: start,  
62:       destination: end,  
63:       travelMode: google.maps.DirectionsTravelMode.DRIVING  
64:      };  
65:              
66:      directionsService.route(request, function(response, status) {  
67:       if (status == google.maps.DirectionsStatus.OK) {  
68:                  //routes  
69:                 var routes = response.routes;  
70:                 for(i = 0; i < routes.length; i++){  
71:                      //legs  
72:                      var legs = response.routes[i].legs;  
73:                      for(j = 0; j < legs.length; j++){  
74:                           //steps  
75:                           var steps = legs[j].steps;  
76:                                for(k = 0; k < steps.length; k++){  
77:                                    //put content in container for driving directions (outputDiv2)  
78:                                     $('#outputDiv2').append('<p>'+steps[k].instructions+'<br /></p>');  
79:                                       
80:                                }  
81:                      }  
82:                 }  
83:                    
84:       }  
85:      });  
86:    
87:    
88:     }  
89:    
90:      calculateDistances();  
91:    </script>  



There are numerous methods and parameters these services offer to better present your results which are not indicated here. Visit Google Maps API Web Services to know more...

vendredi 13 mai 2011

How to Handle the 'CDATA Node' When using XMLReader

XMLReader is a breakthrough for processing large XML data as compared to the DOM reader which needs to load all the data in memory. This snippet prevents the read() function from returning a null value when the data is embedded in CDATA Nodes. The trick is to call the XMLReader::read() method until you escape the void string. More on XMLReader at http://www.php.net/manual/en/class.xmlreader.php

1:  <code>  
2:    
3:  <?php  
4:    
5:  #Snip Tats  
6:  #13/5/2011  
7:  #11.35pm  
8:    
9:  $products = array();  
10:  $i=0;  
11:  $xmlReader = new XMLReader();  
12:  $xmlReader-&gt;open('cyril.xml');  
13:  while($xmlReader-&gt;read()) {  
14:    
15:  // check to ensure nodeType is an Element not attribute or #Text   
16:  if($xmlReader-&gt;nodeType == XMLReader::ELEMENT) {  
17:  //skip the first node which is the parent node for each product  
18:  $xmlReader-&gt;read();  
19:    
20:  //read all child nodes  
21:  while($xmlReader-&gt;read()){  
22:  $value = $xmlReader-&gt;value;  
23:    
24:  //if value was embeded in CDATA node then read this value  
25:  if($xmlReader-&gt;nodeType == XMLReader::CDATA){  
26:  $value = $xmlReader-&gt;value;  
27:  $xmlReader-&gt;read();  
28:  }  
29:    
30:  //end of product node - exit node  
31:  if($xmlReader-&gt;nodeType == XMLReader::END_ELEMENT &amp;&amp; $xmlReader-&gt;localName == 'produit')  
32:  break;  
33:    
34:  //store your product data   
35:  $products[$i][$xmlReader-&gt;localName] = $value;  
36:    
37:  }  
38:  }  
39:  }  
40:    
41:  $xmlReader-&gt;close();  
42:  var_dump($products);  
43:    
44:  ?></code>  

Hope this helps someone some day.

mardi 10 mai 2011

CI 2.0

I have waited unendingly for the new third party facility now available in code igniter 2.0, permitting custom packages and plugins! There is always more

jeudi 28 avril 2011

MooTools

My Object Oriented "Javascript" Tools (MooTools). Migrate from structural Insanity to Object-Oriented JS.

visit http://www.mootools.net for more..

mardi 25 janvier 2011

Redemption Song..

(double click to play - 5secs ad b4 sound :))

vendredi 24 décembre 2010

[FUN]: Math Trick

Trick:-

Choose a number between 2 and 9 inclusive!

1) Multiply by 2
2) Add 5
3) Multiply by 50
4) IF you already had your birthday this year, add 1760 ELSE add 1759.
5) Subtract the year you were born!
6) Check your result!

If your arithmetic is OK then you should have a three digit number! The first digit is the number you chose and the last two digits is your age!! Hope was fun for you!

Question: This works only for 2010. Can you suggest how this trick can adjusted in 2011 so that it does not fail? [comment to answer]