Getting lat / long location using google.maps.geocoder

I tried using some ajax to save the placement in my application and stumbled upon the following code when stack overflow

function getLatLong(address) { var geocoder = new google.maps.Geocoder(); var result = ""; geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { result[lat] = results[0].geometry.location.Pa; result[lng] = results[0].geometry.location.Qa; } else { result = "Unable to find address: " + status; } }); return result; } 

my problem is that when I call the function, it returns nothing, and when I debug and set breakpoints in chrome, it breaks down on the return result before it breaks the result [lat] = results [0] .geometry. location.Pa;

I know that an array should be declared as an array of types, but even when I just returned the results [0] .geometry.location object nothing was returned

what can i do to return lat / long from location so that i can store in my db?

+16
javascript jquery google-maps-api-3 geocoding
Jan 10 2018-12-12T00:
source share
4 answers

The problem you are facing is that you treat the geocoder.geocode function as immediately exiting before you get the return result. What really happens is that geocoder.geocode is launched, and then you get an immediate return result. Since the asynchronous result is most likely not returned, your result is empty. Think of the result of geocoding as a push rather than traction. The storeResult function, not shown, is the code you need to do to save the information. Since you are combining the result with an error string, you must handle this in the storeResult function. Alternatively, you may have status as a result that indicates succcess or failure.

 function getLatLong(address) { var geocoder = new google.maps.Geocoder(); var result = ""; geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { result[lat] = results[0].geometry.location.Pa; result[lng] = results[0].geometry.location.Qa; } else { result = "Unable to find address: " + status; } storeResult(result); }); } 
+16
Jan 10 2018-12-12T00:
source share

This is not an answer, but do not use Pa and Qa, always use the lng () and lat () functions:

  place.geometry.location {...} Pa: 56.240477 Qa: -0.902655999999979 toString: function(){return"("+this.lat()+", "+this.lng()+")"} equals: function(a){return!a?k:Cd(this.lat(),a.lat())&&Cd(this.lng(),a.lng())} lat: function(){return this[a]} lng: function(){return this[a]} toUrlValue: function(a){a=Hd(a)?a:6;return $d(this.lat(),a)+","+$d(this.lng(),a)} 
+20
Jan 15 2018-12-12T00:
source share

I struggled with this and found the following to work.

  • Scroll through the object and enter a new array:

     var newArray = []; for (var key in latLng) { newArray.push(key); } 
  • Call your object with variable values ​​using square bracket syntax, dotted marking:

     var lat = latLng[newArray[0]]; var lng = latLng[newArray[1]]; 
+2
Feb 19 '13 at 21:02
source share

In my case, the simplest solution to get latitude and longitude from a place object was:

  var latitude=place.geometry.location['k']; var longitude=place.geometry.location['A']; 
0
Mar 27 '14 at 18:28
source share



All Articles