1

我在获取经度和纬度值地理编码然后在信息窗口中显示相关地址时遇到问题。我尝试了多种方法都没有成功,但是我必须承认我对 javascript 不是很熟悉。我不断地找回价值:“未定义”。

这是我的代码片段,显示了主要组件:

var position = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var geocoder = new google.maps.Geocoder(); var address;

if (geocoder) {
    geocoder.geocode({ 'latLng': position }, function (results, status) {       
       if (status == google.maps.GeocoderStatus.OK) {
            address = (results[0].formatted_address);
       } else { 
            address = (position.coords.latitude + ', ' +  position.coords.longitude);
       }           
    }); 
}

var info = 
          ('<span class="txt_bld">Location:</span> '         + address                      + '<br />' +
          '<span class="txt_bld">Accuracy:</span> '          + position.coords.accuracy     + '<br />' +
          '<span class="txt_bld">Time:</span> '              +  position.timestamp);

谁能告诉我如何将 lat/lng 位置转换为地址以便在我的信息窗口中显示它们?

编辑

更新代码:
var position = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); var geocoder = new google.maps.Geocoder(); var infowindow = new google.maps.InfoWindow(); var address;

if (geocoder) {
    geocoder.geocode({ 'latLng': position }, function (results, status) {       
       if (status == google.maps.GeocoderStatus.OK) {
            address == (results[0].formatted_address);
       } else { 
            address == (position.coords.latitude + ', ' +  position.coords.longitude);
       }    
       var info = 
          ('<span class="txt_bld">Location:</span> '         + address                      + '<br />' +
          '<span class="txt_bld">Accuracy:</span> '          + position.coords.accuracy     + '<br />' +
          '<span class="txt_bld">Time:</span> '              + position.timestamp); 

        if(!infowindow){
            infowindow = new google.maps.InfoWindow({
                content: info
            });
        }else{
            infowindow.setContent(info);
        }

        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);    
          setTimeout(function () { 
            infowindow.close(); 
          }, 5000);
        });            
    }); 
}

if(!marker){ marker = new google.maps.Marker({ position: position, map: this.map, icon: markericon, draggable:false }); }else{ marker.setPosition(point);
}

4

2 回答 2

1

地理编码器是异步的。您需要在回调函数中使用它返回的数据。像这样的东西(未经测试):

var position = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 
var geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();
var address;

if (geocoder) {
  geocoder.geocode({ 'latLng': position }, function (results, status) {       
   if (status == google.maps.GeocoderStatus.OK) {
        address = (results[0].formatted_address);
   } else { 
        address = (position.coords.latitude + ', ' +  position.coords.longitude);
   }           
   var info = 
      ('<span class="txt_bld">Location:</span> '         + address
       + '<br />' +
      '<span class="txt_bld">Accuracy:</span> '          + position.coords.accuracy
      + '<br />' +
      '<span class="txt_bld">Time:</span> '              +  position.timestamp);
    infowindow.setContent(info);
    infowindow.setPosition(position);
    infowindow.open(map);
  }); 
}

工作示例

于 2012-12-22T12:43:55.207 回答
0

我们使用地理编码器通过地址获取纬度,因为我们没有纬度,所以我们需要通过地理编码器找到纬度。如果我们有纬度长坐标,那么我们可以直接使用

 var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
            bounds.extend(position);
            marker = new google.maps.Marker({
                position: position,
                map: map,
                title: markers[i][0]
            });
 

于 2021-05-27T19:20:08.073 回答