1

I've have been trying to generate lat/long from an address provided by a URL variable and display on a Google map, but the map is blank (lat/long clearly undefined). The problem seems to lie in the getCoordinates function, but I can't figure out what it is.

My code:

<?php $address = $_GET["address"]; ?>

<script>
geocoder = new google.maps.Geocoder();

window.getCoordinates = function (address, callback) {
    var coordinates;
    geocoder.geocode({ address: address}, function (results, status) {
        coords_obj = results[0].geometry.location;
        coordinates = [coords_obj.nb,coords_obj.ob];
        callback(coordinates);
    })
}
</script>

<script>

google.maps.visualRefresh = true;
var map;

function initialize() {


    getCoordinates('<?php echo $address ?>', function(coords) {
      var mapOptions = {
      center: new google.maps.LatLng(coords[0], coords[1]),
      zoom: 15,
      mapTypeId: google.maps.MapTypeId.ROADMAP
      };

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

})

}

google.maps.event.addDomListener(window, 'load', initialize);

Any help would be appreciated. Thanks.


***Found the problem:

I was using coords_obj.nb and coords_obj.ob when I should have been using coords_obj.A and coords_obj.F as output by my console.log:

[Object]
0: Object
address_components: Array[8]
formatted_address: "1045 Mission Street, San Francisco, CA 94103, USA"
geometry: Object
location: pf
A: 37.7800206
F: -122.40959459999999
__proto__: pf
location_type: "ROOFTOP"
viewport: ph
__proto__: Object
place_id: "ChIJeaMNfoSAhYARY4zPZwV_vgw"
types: Array[1]
__proto__: Object
length: 1
__proto__: Array[0]
4

1 Answer 1

2

Thank you geocodezip. To avoid breakage on future API updates, I replaced with:

geocoder = new google.maps.Geocoder();

window.getCoordinates = function (address, callback) {
    var coordinates;
    geocoder.geocode({ address: address}, function (results, status) {
        coords_obj_lat = results[0].geometry.location.lat();
        coords_obj_lng = results[0].geometry.location.lng();
        coordinates = [coords_obj_lat,coords_obj_lng];
        callback(coordinates);

    })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.