-1

what i need is to get ...for example, London 's lat and lng.
so i pass in a string 'london' into a function and then the function returns me lat, and lng, so i can set up 'center' location as london in google map.

here is piece of my code:

 function initialize() {
 // i have below line working, as i was passing in lat and lng value directly....
//var latitudeLongtitude = new google.maps.LatLng(centreLatitude, centreLongitude)
 // now i m trying to pass in a string - 'london'  , it is not working....
           

          var latitudeLongtitude = new google.maps.LatLng(getLatLong("London").lat(), getLatLong("London").lng());


            var mapOptions = {
              zoom: zoom,
              center: latitudeLongtitude,
              mapTypeId: mapType
            };

            var map = new google.maps.Map(document.getElementById('map-canvas'),
                mapOptions);

            var marker = new google.maps.Marker({
                position: latitudeLongtitude,
                map: map,
                title: 'Hello World!',
                icon: markersImage
            });
          }
    google.maps.event.addDomListener(window, 'load', initialize);


function getLatLong(address){
        var geo = new google.maps.Geocoder;

        geo.geocode({'address':address},function(results, status){
                if (status == google.maps.GeocoderStatus.OK) {
                  return results[0].geometry.location;
                } else {
                  alert("Geocode was not successful for the following reason: " + status);
                }

         });

    }

basically, i was trying to modfied code base on this example

please anyone could help me with CODE example, thanks.....

Community
  • 1
  • 1
sefirosu
  • 2,558
  • 7
  • 44
  • 69
  • And do you have a valid API key etc. [**Read this**](https://developers.google.com/maps/documentation/business/geolocation/) and follow the TOS – adeneo Jul 28 '14 at 15:32
  • yes....i have a valued api key – sefirosu Jul 28 '14 at 15:35
  • The [example](http://stackoverflow.com/questions/3617227/how-can-i-get-latitude-longitude-of-a-location-programmatically-or-using-a-api) you are referring to is incorrect, the geocoder is [asynchronous](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron), you can't return the results from the callback function. – geocodezip Jul 28 '14 at 15:43

1 Answers1

0

As google.maps.Geocoder is asynchronous method you need to call initialize() after it has received response:

geo.geocode({'address':address},function(results, status){
    if (status == google.maps.GeocoderStatus.OK) {
        initialize( results[0].geometry.location.lat, results[0].geometry.location.lng );
    } else {
        alert("Geocode was not successful for the following reason: " + status);
    }
});

And change your initialize() function to accept lat and lng params:

function initialize( lat, lng ) {
    var latitudeLongtitude = new google.maps.LatLng( lat, lng );
//....

And after that you will be able to initialize your map by calling:

getLatLong( 'London' );
antyrat
  • 27,479
  • 9
  • 75
  • 76
  • sry...im getting confused....how exactly? so i have geo.geocode method 1st, then initialize at 2nd place, then function getLatLong(address) at 3td place? – sefirosu Jul 28 '14 at 15:55
  • where exactly do i put getLatLong( 'London' ); ? cheers... – sefirosu Jul 28 '14 at 15:57
  • You need to put it instead of "initialize()" call. I don't know where are you call it. In body tag probably or window.load method – antyrat Jul 28 '14 at 15:58