【问题标题】:InfoWindow & GeoCoder信息窗口和地理编码器
【发布时间】:2012-12-22 09:44:18
【问题描述】:

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

这是我的代码的 sn-p,显示了主要组件:

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

【问题讨论】:

    标签: javascript jquery google-maps-api-3 google-geocoder


    【解决方案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);
      }); 
    }
    

    Working example

    【讨论】:

    • 我已经更新了我的问题中的代码。这就是我想出的,但是我仍然将位置设为未定义
    • “位置”在哪里未定义?标记在哪里定义?你能提供一个展示问题的jsfiddle吗?
    • 标记在 if(geocoder) 之后定义。我更新了上面的代码。在 html 中,当我单击标记并获取信息窗口时,我得到以下信息: Location: undefined Accuracy: 35 Etc..
    • 你的代码和我的不一样。 “==”与“=”不同。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多