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]