cbronson
cbronson

Reputation: 386

Meteor and Google Maps API: Geocode not working

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:

Console Output

But the response from the Google Maps API does, in fact, contain the needed information:

HTTP Response

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

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

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 degrees

Example

codeAddress("3100 East Fletcher Avenue, Tampa, FL, United States", function (result) {
    console.log(result.toString());
  });

Upvotes: 1

Related Questions