帮助用户位置附近的地方Google JS API

时间:2012-12-26 19:48:09

标签: javascript google-maps geolocation

我想向所有博物馆展示用户的地理位置。我想在用user_lat_long替换init中的latLng时遇到问题。出于某种原因,在init中使用alert时,user_lat_long值未定义。你能解释一下我做错了什么吗?我还是JS的新手。谢谢!

function init() {
  var latLng = new google.maps.LatLng(44.43, 26.11);
  Demo.map = new google.maps.Map(Demo.mapContainer, {
    zoom: 13,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP});
    var request = {
      location: latLng,
      radius: 50000,
      types: ['museum']
    };
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(Demo.map);
    service.nearbySearch(request, function (results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
            {
                 var placeLoc = results[i].geometry.location;
                 var marker = new google.maps.Marker({
                   map: Demo.map,
                   position: placeLoc
                 });

                 google.maps.event.addListener(marker, 'click', function() {
                   infowindow.setContent(restults[i].name);
                   infowindow.open(map, this);
                 });
               }
          }
        }
      });

 }

google.maps.event.addDomListener(window, 'load', init);
function pozitie() {
    if(navigator.geolocation) {

     navigator.geolocation.getCurrentPosition(currentPositionCallback);

     } else {

     alert('The browser does not support geolocation');

 }
function currentPositionCallback(position) {

user_lat_long = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

 var marker = new google.maps.Marker({
 position: user_lat_long,
 map: Demo.map,
 icon: image
 });

 Demo.map.setCenter(user_lat_long);
 Demo.map.setZoom(15);

 }

1 个答案:

答案 0 :(得分:0)

我认为你错过了pozitie函数的结束括号,这里是修改后的代码。我相信您在服务器中运行此代码(而不是本地HTML文件),否则您将无法获取用户位置。

function init() {
  var latLng = new google.maps.LatLng(44.43, 26.11);
  Demo.map = new google.maps.Map(Demo.mapContainer, {
    zoom: 13,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP});
    var request = {
      location: latLng,
      radius: 50000,
      types: ['museum']
    };
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(Demo.map);
    service.nearbySearch(request, function (results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
            {
                 var placeLoc = results[i].geometry.location;
                 var marker = new google.maps.Marker({
                   map: Demo.map,
                   position: placeLoc
                 });

                 google.maps.event.addListener(marker, 'click', function() {
                   infowindow.setContent(restults[i].name);
                   infowindow.open(map, this);
                 });
               }
          }
        }
      });

 }

google.maps.event.addDomListener(window, 'load', init);
google.maps.event.addDomListener(window, 'load', pozitie);//Assuming you are calling this method after init is called
function pozitie() {
    if(navigator.geolocation) {

     navigator.geolocation.getCurrentPosition(currentPositionCallback);

     } else {

     alert('The browser does not support geolocation');

 }
}
function currentPositionCallback(position) {

var user_lat_long = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
 var marker = new google.maps.Marker({
 position: user_lat_long,
 map: Demo.map
 });

 Demo.map.setCenter(user_lat_long);
 Demo.map.setZoom(15);

 }