2016-12-03 49 views
1

我已经尝试过这里的示例,但无法获取html5地理位置坐标。 如果我硬编码的坐标,它工作正常。无法获取html5地理位置坐标来工作

var infowindow; 
    var map; 

function initialize() { 

    var latlng = new google.maps.LatLng(53.3452572,-6.2648937); 

    map = new google.maps.Map(document.getElementById("map-canvas"), { 
     center: latlng, 
     zoom: 10, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 

    }); 

var request = { 
location: latlng, 

radius: 500000, 

types: ['pet_shop','pet_store'] 
}; 

但是,如果我尝试使用以下我得到一个错误ReferenceError:位置未定义。我在这里错过什么或做错了什么?

lat = position.coords.latitude; 
    long = position.coords.longitude; 
    var latlong = new google.maps.LatLng(lat, long); 
+0

您正在使用地理定位API? – Dherya

+0

你在哪里调用'getCurrentPosition'? – sabithpocker

回答

1

watchPosition or getCurrentPosition应该用于获取回调中的位置。这里是来自MDN的similar example页面

function initialize() { 
     navigator.geolocation.getCurrentPosition(function(position) { 
     var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     map = new google.maps.Map(document.getElementById("map-canvas"), { 
      center: latlng, 
      zoom: 10, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      }); 
}); 
+0

谢谢sabithpocker,工作! – user2110655

+0

如果要在用户更改位置时更新位置,请使用watchPosition。 – sabithpocker