0

我想显示用户地理位置附近的所有博物馆。当我想用 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);

 }
4

1 回答 1

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);

 }
于 2012-12-26T20:22:34.617 回答