Mark H
Mark H

Reputation: 589

Uncaught TypeError: Cannot read property 'latitude' of undefined

Hi all I have the following code:

  var map;
  var infowindow;


  function getLocation()
    {
    if (navigator.geolocation)
      {
      navigator.geolocation.getCurrentPosition(initialize);
      }
    else{x.innerHTML="Geolocation is not supported by this browser.";}
    }

  function initialize(position) {
    var pyrmont = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

    map = new google.maps.Map(document.getElementById('map'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: pyrmont,
      zoom: 15
    });

    var request = {
      location: pyrmont,
      radius: 500,
      types: ['restaurant']
    };
 ...

Basically I can get the map to work great if I set the Long/Lat co-ordinates but instead I want these to be passed by getting the users location. With the code above the map is displaying on my phone but with the following error:

TypeError: 'undefined' is not an object (evaluating 'position.coords.latitude')

but on the desktop I get no map and the following error:

   Uncaught TypeError: Cannot read property 'latitude' of undefined 

Any help would be great I'm a newbie to Javascript and these APis

Upvotes: 6

Views: 19717

Answers (2)

Louis Ricci
Louis Ricci

Reputation: 21116

Well it's not an issue with the geolocation API. Here's a JSFIDDLE of geolocation: http://jsfiddle.net/w4nvZ/3/

There's also the documentation and examples for the API here: http://www.w3schools.com/html/html5_geolocation.asp

Google even has an example of geolocation working with the JavaScript Google Maps API: https://google-developers.appspot.com/maps/documentation/javascript/examples/map-geolocation

 var map;
function initialize() {
var mapOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Location found using HTML5.'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize); 

Upvotes: 1

BernzSed
BernzSed

Reputation: 1139

Later in your code, on your page at http://markhaynes.me/mwp/addloc.html, you're calling google.maps.event.addDomListener(window, 'load', initialize);

This causes Google to call the initialize() function, but it passes an event object as the position parameter instead of the position object you expected.

Upvotes: 5

Related Questions