user1371896
user1371896

Reputation: 2230

Getting latitude & longitude values of different places to a value

I am trying to find a set of latitude longitude values and assign it to a variable. My code looks like this

var k = findLattitudeLongitude("Italy");
console.log(k) // comes undefined
function findLattitudeLongitude(input){
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': input}, function(results, status) {
      var location = results[0].geometry.location;
        return location;

    });
   }

but it comes as undefined. What is the best way to achieve the above requirement. Is there any other method?

Upvotes: 0

Views: 430

Answers (2)

Khaled Hasania
Khaled Hasania

Reputation: 345

it's strange that the function return undefined, what you can do is to call another function, this way work with me fine, you can pass latitude and longitude params like the code below or the whole object

$( document ).ready(function() {
function   getlatlan(){
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': "Italy"}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
         dosome(results[0].geometry.location.nb,results[0].geometry.location.mb)
     return true;

    ;
}});
}
function dosome(lat,lng){
console.log(lat,lng);
}
getlatlan();
});

Upvotes: 1

Praveen
Praveen

Reputation: 56509

Actually what you've done is correct.

The problem is the response received from Google geocoder is taking some time.

I've created a fiddle for a better understanding.

Upvotes: 1

Related Questions