簡體   English   中英

如何在信息窗口中顯示地址的地址? 谷歌地圖API

[英]How can I display the address of my geocode in an info window? Google maps API

我通過查找經度和緯度並將結果返回到我的頁面以在我的數據庫中使用來使用地理位置來獲取我的Google地圖API上的地址。

我無法在地圖中的infowindow中顯示這些結果。

我如何使用下面的代碼包括我的infowindow中的地址。 它目前顯示long和lat,代碼如下:

infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>'); 
infowindow.open(map, marker);

函數geocodePosition查找坐標的地址並將其附加到我的表單中的html輸入。

我怎樣才能使它適應更新信息窗口。 我現在嘗試了很多方法但沒有成功。 有任何想法嗎??

<script type="text/javascript">
var geocoder = new google.maps.Geocoder();

function geocodePosition(pos) {
  geocoder.geocode({
    latLng: pos
  }, function(responses) {
    if (responses && responses.length > 0) {
      updateMarkerAddress(responses[0].formatted_address);

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

function updateMarkerStatus(str) {
  document.getElementById('markerStatus').value = str;
}

function updateMarkerPosition(latLng) {
  document.getElementById('info').value = [
    latLng.lat(),
    latLng.lng()
  ].join(', ');
}

function updateMarkerAddress(str) {
  document.getElementById('address').value = str;
}


function initialize() { 

  var latLng = new google.maps.LatLng(-34.397, 150.644);
  var map = new google.maps.Map(document.getElementById('mapCanvas'), {
    zoom: 8,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });


  var marker = new google.maps.Marker({
    position: latLng,
    title: 'Property location marker',
    map: map,
    draggable: true
  });

        var input = document.getElementById('searchTextField');
        var autocomplete = new google.maps.places.Autocomplete(input);
        var infowindow = new google.maps.InfoWindow();

        autocomplete.bindTo('bounds', map);

google.maps.event.addListener(autocomplete, 'place_changed', function() {
  var place = autocomplete.getPlace();
  if (place.geometry.viewport) {
    map.fitBounds(place.geometry.viewport);
  } else {
    map.setCenter(place.geometry.location);
    map.setZoom(17);
  }
  var image = new google.maps.MarkerImage(
      place.icon, new google.maps.Size(71, 71),
      new google.maps.Point(0, 0), new google.maps.Point(17, 34),
      new google.maps.Size(35, 35));
  marker.setIcon(image);
  marker.setPosition(place.geometry.location);

    infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>'); 
    infowindow.open(map, marker);

});

  // Update current position info.
  updateMarkerPosition(latLng);
  geocodePosition(latLng);

    infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>'); 
    infowindow.open(map, marker);

  // Add dragging event listeners.
  google.maps.event.addListener(marker, 'dragstart', function() {
    updateMarkerAddress('Dragging...');
  });

  google.maps.event.addListener(marker, 'drag', function() {
    updateMarkerStatus('Dragging...');
    updateMarkerPosition(marker.getPosition());
  });

  google.maps.event.addListener(marker, 'dragend', function() {
    updateMarkerStatus('Drag ended');
    geocodePosition(marker.getPosition());

        //Show property address in window
    infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>'); 
    infowindow.open(map, marker);
  });
}

// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
</script>

marker.getPosition()不會提供街道地址。 根據您當前的代碼,最好的選擇是移動:

infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>');

geocodePosition()函數,然后傳遞infowindow對象作為第二個參數,以該功能。

因此,在initialize()函數中,您可以通過包含第二個參數來修改對geocodePosition的調用:

function initialize() {

...

  geocodePosition(marker.getPosition(), infowindow);

}

然后在你的geocodePosition()函數中,你接受第二個參數,並用responses[0].formatted_address value更新它。 我添加了變量,以使傳遞地址和消息的值更容易。

function geocodePosition(pos, infowindow) {
  geocoder.geocode({
    latLng: pos
  }, function(responses) {
    if (responses && responses.length > 0) {
      var address = responses[0].formatted_address;      
      updateMarkerAddress(address);
      infowindow.setContent('<div><strong>' + address + '</strong><br>');

    } else {
      var msg = 'Cannot determine address at this location.';      
      updateMarkerAddress(msg);
      infowindow.setContent('<div><strong>' + msg + '</strong><br>');
    }
  });
} 

您可能要使用Google Map api- http://code.google.com/apis/maps/documentation/geocoding/#GeocodingRequests

還有其他API可以為您提供相同的API-但大多數API都需要花錢,而使用Google,您可以免費獲得很多“里程”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM