Reputation: 386
I am attempting to Geocode a physical address using the Google Maps API in a Meteor template.
This is the function I am using to run the geocoder:
codeAddress = function(address, callback) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
callback(results[0].geometry.location);
} else {
callback(0);
}
});
}
Using a static address for testing purposes, this is the function I am using to call codeAddress():
codeAddress("3100 East Fletcher Avenue, Tampa, FL, United States", function(result) {
console.log(result);
});
Here is the Problem
The console output returns nothing for the Lat and Long:
But the response from the Google Maps API does, in fact, contain the needed information:
I know that the API is asynchronous but I don't know enough about Meteor to figure out how to retrieve the data after it has been returned by the API.
Upvotes: 1
Views: 359
Reputation: 59358
There is nothing wrong with the provided codeAddress
function, the reason why lat/lng values of results[0].geometry.location
object are not printed in the console is because results[0].geometry.location
of LatLng type
which in turn does not expose lat
and lng
properties.
To print value representation of LatLng class
you could utilize the following functions:
toString()
- Converts to string representation.toUrlValue(precision?:number)
- Returns a string of the form "lat,lng" for this LatLng. We round the lat/lng values to 6 decimal places by default.lat()
or lng()
- Returns the latitude or longitude in degreesExample
codeAddress("3100 East Fletcher Avenue, Tampa, FL, United States", function (result) {
console.log(result.toString());
});
Upvotes: 1