在infowindow中显示反向地理编码地址

时间:2015-07-29 13:01:18

标签: javascript google-maps

我希望点击infowindow时显示地址,但我不确定如何将标记标题分配给我使用

检索的反向地理编码地址
responses[0].formatted_address

以下是一些代码:

function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {

  updateMarkerAddress(responses[0].formatted_address);

  //ADDRESS HERE
  addressDisplay = responses[0].formatted_address;      

   var info = 
  ('<span class="txt_bld">Location:</span> ' + addressDisplay);
infowindow.setContent(info);
infowindow.setPosition(position);
infowindow.open(map);


} else {
  updateMarkerAddress('Cannot determine address at this location.');
}
  });
}

另一节

function initialize() {

if (navigator.geolocation){

navigator.geolocation.getCurrentPosition(function (p) {
var latLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);

 var map = new google.maps.Map(document.getElementById('mapCanvas'), {
   zoom: 14,
   center: latLng,
   mapTypeId: google.maps.MapTypeId.ROADMAP


 });

 var marker = new google.maps.Marker({
   position: latLng,
   //Address should appear when clicked
   title: 'You are here',
   map: map,
   draggable: true  
 });

  google.maps.event.addListener(marker, "click", function (e) {

  var infoWindow = new google.maps.InfoWindow();

   infoWindow.setContent(marker.title);
   infoWindow.open(map, marker);

   });

1 个答案:

答案 0 :(得分:0)

您可以在初始化标记时执行地理编码。

var marker = new google.maps.Marker({
  //marker params
});
var info = new google.maps.InfoWindow({
  //infoWindow params
});
geocoder.geocode({location:marker.getPosition()}, function(results, status){
  if(status == 'OK'){
    marker.setTitle(results.formatted_address);
    info.setContent("<div>"+results.formatted_address+"</div>");
  }
});

基本上,一旦反向地理编码完成,在回调中,设置标记的标题(或其他任何内容;我不清楚你想要做什么)。