首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InfoWindow & GeoCoder

InfoWindow & GeoCoder
EN

Stack Overflow用户
提问于 2012-12-22 09:44:18
回答 2查看 1K关注 0票数 1

我有一个问题,获取经度和纬度值的地理编码,然后显示相关的地址在一个信息窗口。我尝试过多种方法,但都没有成功,但我必须承认,我对javascript不太熟悉。我不断地以价值的形式回来:“未定”。

下面是我的代码片段,其中显示了主要组件:

代码语言:javascript
复制
 `var position = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);     var geocoder = new google.maps.Geocoder();     var address;`
代码语言:javascript
复制
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翻译成一个地址,以便在我的贴图中显示出来吗?

编辑

经修订的守则:

代码语言:javascript
复制
  `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;`
代码语言:javascript
复制
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
复制
 `}`
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-22 12:43:55

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

代码语言:javascript
复制
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);
  }); 
}

工作实例

票数 1
EN

Stack Overflow用户

发布于 2021-05-27 19:20:08

我们正在使用地理编码器获得Lat long的地址,因为我们没有Lat long,所以我们需要通过地理编码器找到Lat long。如果我们有Lat长坐标,那么我们可以直接使用

代码语言:javascript
复制
 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]
            });
代码语言:javascript
复制

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14001753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档