我是谷歌地图的新手,我使用 html5 来获取用户的纬度和经度坐标,但我想获取用户最近的地名,以便他可以选择,(如果我们可以点击,为什么要输入?)我用谷歌搜索和搜索,但是不能做任何事情。我今天写了这些脚本....
<!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
这样我就可以找到地名,有人可以帮我吗?我发现这个链接可能会有所帮助,但我无法弄清楚,我不需要地图中的地点信息,但我想在变量中获取地名
在此先感谢..如果问题不清楚,请提及我,我会尝试更清楚,,,我为我的语言道歉....