2013-08-31 43 views
0

我從Google API獲取了此代碼,我想提取該位置的經緯度,以便我可以找到它周圍的最近的10個位置。

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

<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把一個標記上的谷歌地圖。我怎麼能分別得到經緯度傳遞給html身體

我想要做的是在獲得座標後,我將搜索距離數據庫最近的10個位置(經緯度爲字段)。然後,我怎麼能把標記放在地圖上最近的10個位置?請幫幫我。謝謝!

+0

這個代碼是你的全部嗎? – putvande

+0

我添加了正文。 @putvande –

+0

我的意思是,你有代碼來查詢數據庫,並得到結果回來等 – putvande

回答

0

的經度和緯度在results[0]但在results[0].location.lat()results[0].location.lng()舉行什麼。這些可以通過指定的元素,你想將它們應用到,並使用該信息的HTML被傳遞迴HTML,是這樣的:

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

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

這裏有一對夫婦的關係是快速的函數

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 = []; 
} 

爲了標記添加到您需要定義它,沿着東西線陣列(假設你的數組稱爲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中找到。

0

這是probabbly你需要

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

    }); 

} 
相關問題