1

進出口新的谷歌地圖,我用HTML5來獲得的緯度和經度的用戶cordinates,但我想獲得用戶的最近的地方名稱,以便他可以選擇,(爲什麼類型如果我們可以點擊?)我谷歌搜索,但不能做任何事情。我今天寫了這些腳本....cordinates到地名使用谷歌地圖API V3

<!DOCTYPE html> 
<html> 
<body> 
<p id="demo">Click the button to get your position:</p> 
<button onclick="getLocation()">Try It</button> 
<div id="mapholder"></div> 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true"> 
//only a v3 api library 
</script> 
<script src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script> 


var x=document.getElementById("demo"); 
function getLocation() 
    { 
    if (navigator.geolocation) 
    { 
    navigator.geolocation.getCurrentPosition(showPosition,showError); 
    } 
    else{x.innerHTML="Geolocation is not supported by this browser.";} 
    } 

function showPosition(position) 
    { 
    lat=position.coords.latitude; 
    lon=position.coords.longitude; 
    latlon=new google.maps.LatLng(lat, lon) 

    mapholder=document.getElementById('mapholder') 
    mapholder.style.height='250px'; 
    mapholder.style.width='500px'; 

    var myOptions={ 
    center:latlon,zoom:18, 
    mapTypeId:google.maps.MapTypeId.ROADMAP, 
    mapTypeControl:false, 
    navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL} 
    }; 
    var map=new google.maps.Map(document.getElementById("mapholder"),myOptions); 
    var marker=new google.maps.Marker({position:latlon,map:map,title:"Some where here"}); 

var geocoder = new google.maps.Geocoder(); 
geocoder.geocode({latLng:latlon},function(results, status) { 

      if (status == google.maps.GeocoderStatus.OK) { 
      if (results[0]) { 
       infoWindow.setContent(results[0].formatted_address); 
       infoWindow.setPosition(event.latLng); 
       infoWindow.open(map); 
      } 
      } 
     }); 
    } 
function showError(error) 
    { 
    switch(error.code) 
    { 
    case error.PERMISSION_DENIED: 
     x.innerHTML="User denied the request for Geolocation." 
     break; 
    case error.POSITION_UNAVAILABLE: 
     x.innerHTML="Location information is unavailable." 
     break; 
    case error.TIMEOUT: 
     x.innerHTML="The request to get user location timed out." 
     break; 
    case error.UNKNOWN_ERROR: 
     x.innerHTML="An unknown error occurred." 
     break; 
    } 
    } 
</script> 
</body> 
</html> 

我已經做了所有這些,到目前爲止,現在我需要做的是,我想通過這兩個變量給出的位置地名: lat,lon讓我能找到的地名,任何一個可以幫助我嗎? 我發現這個鏈接,可能是有益的,但我不能圖出來,,,我不需要在地圖中placeinfo,但我想在先進的可變

由於得到地名。在情況問題是不明確的,請註明我,和虐待嘗試更加清晰,,,,我爲我的語言道歉....

回答

0

你已經得到所有你需要的東西,地理編碼型結果。 (導致地理編碼()內 -callback)

那些結果(一個陣列)由不同的物品,它們中的一個應該有一個構件類型(也是一個陣列),其中第二項被設爲爲「地方」,這通常標誌着該項目作爲一個城市。當你找到這個項目時,項目的long_name - 成員會給你城市名稱。

更多信息:https://developers.google.com/maps/documentation/javascript/geocoding?hl=en#GeocodingAddressTypes

相關代碼的註釋:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true"> 
//only a v3 api library 
</script> 
<script src="http://maps.google.com/maps/api/js?sensor=false"></script> 

你加載地圖的API兩次,因爲第一個<script/>將加載地圖的API 的地庫。你最好刪除第二個<script/>

+0

感謝腳本雙重問題,我很抱歉,因爲我可以讓你..當我提醒結果變量,它只顯示對象...我的意思是我將如何從文本它?請幫幫我。進出口新的JavaScript來...但是,我沒有再次感謝知道結果究竟包含我想要的......現在期待着該對象轉換成數組 – acidburn 2012-04-04 03:52:22

+0

我想這: '警報(results.formatted_address)/ /說undefined 和其他這些也 警報(結果[0])//它說對象 alert(results.street_address)//它說undefined alert(results.locality)//它也說undefined' 但什麼都沒有發生每次驚動對象 – acidburn 2012-04-04 04:15:44

+0

一次使用JSON到對象轉換爲文本,但couldnot發現任何財產或可能是我失去了一些東西。我這樣做: '警報(JSON.stringify(結果))' ,但我不能做任何事情,請幫助 – acidburn 2012-04-04 04:48:37