簡體   English   中英

使用Google API獲取位置的緯度和經度並傳遞變量

[英]Getting latitude and longitude of a location using Google API and passing of variables

我是從Google API獲得此代碼的,我想提取該位置的緯度和經度,以便可以找到其附近的10個最近位置。

Javascript:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=mykeyhere&sensor=true"></script> 
<script type="text/javascript">
var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(14.5833, 120.9667);
    var mapOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

</script>


身體:

<body onload="initialize()">
 <div id="map-canvas" style="width: 320px; height: 480px;"></div>
   <div>
    <input id="address" type="textbox">
    <input type="button" value="Encode" onclick="codeAddress()">
  </div>
</body>


有一個文本框,用戶將在其中輸入位置,而javascript將標記放在Google地圖上。 如何分別獲得經度和緯度並將其傳遞給html正文

我想要做的是在獲取坐標之后,將在數據庫中搜索最近的10個位置(其中緯度和經度是字段)。 然后如何在地圖上最近的10個位置放置標記 請幫我。 謝謝!

這可能是您需要的

https://developers.google.com/maps/documentation/geocoding/?csw=1 嘗試使用此功能

  function getLatLong(address){
  var geo = new google.maps.Geocoder;

  geo.geocode({'address':address},function(results, status){
          if (status == google.maps.GeocoderStatus.OK) {
            return results[0].geometry.location;
          } else {
            alert("Geocode was not successful for the following reason: " + status);
          }

   });

}

緯度和經度保留在您的results[0]但在results[0].location.lat()results[0].location.lng() 可以通過指定要應用於它們的元素並使用信息填充html來將它們傳遞回html:

document.getElementById('coords').innerHTML(results[0].location.lat() + ', ' + results[0].location.lng());

理想情況下,要向地圖添加新標記,您首先需要設置一個數組來保存您創建的標記。 然后,當您擁有新的標記信息時,將其推入陣列。 這將完成兩件事。 一,標記將自動添加到地圖。 其次,如果您需要以任何方式更改標記(可能是通過從屏幕上清除它們,或者完全刪除它們),則可以對陣列執行操作。

這是與此相關的幾個快速函數(假設您的數組稱為markers

function clearMarkers() { // clears markers but doesn't remove them from the markers array
  for (var i = 0, l = this.markers.length; i < l; i++) {
    this.markers[i].setMap(null);
  }
};

function deleteMarkers() {
  this.markers = [];
}

為了向數組添加標記,您需要對其進行定義,類似於:

function addMarker(latlng) {
  var marker = new google.maps.Marker({
    draggable: false,
    raiseOnDrag: false,
    icon: // icon path
    animation: google.maps.Animation.DROP,
    position: latlng, // google latlng coords
    map: // element holding your map
  });
  markers.push(marker);
}

您需要的所有內容都可以在Google Maps API中找到

暫無
暫無

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

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