从Google地图和HTML 5地理位置获取国家/地区代码 [英] Getting country code from Google Maps and HTML 5 GeoLocation

查看:181
本文介绍了从Google地图和HTML 5地理位置获取国家/地区代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用HTML 5 GeoLocation获取经度和纬度,然后使用Google Maps API获取经度/纬度的国家/地区代码。任何人都可以告诉我在我的代码中哪里出错了,我目前在main.js中得到了Javascript错误'this.lat不是函数':

pre > < html>

< head>

< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.jstype =text / javascript><<< ; /脚本>
< script src =http://maps.google.com/maps?file=api&amp;v=2&amp;key=drnhdhddfhgfghfgtype =text / javascript>< /脚本>

< script type =text / javascript>

if(navigator.geolocation){

navigator.geolocation.getCurrentPosition(function(position){

var lat = position.coords.latitude ;
var lng = position.coords.longitude;

var latlng = new google.maps.LatLng(lat,lng);

$ .post(' //结果){
alert(results);
});
});




}
其他{
alert(地理定位服务不被你的浏览器支持。
}

< / script>

< / head>

< body>



< / body>

< / html>


解决方案

试试这个:

 < html> 

< head>
< script src =http://maps.google.com/maps/api/js?sensor=truetype =text / javascript>< / script>
< script type =text / javascript>

if(navigator.geolocation){

navigator.geolocation.getCurrentPosition(function(position){

var lat = position.coords.latitude ;
var lng = position.coords.longitude;

var latlng = new google.maps.LatLng(lat,lng);

var geocoder = new google .maps.Geocoder();

geocoder.geocode({'latLng':latlng},function(results,status){
if(status == google.maps.GeocoderStatus.OK ){
if(results [1]){
alert(results [7] .formatted_address);
}
} else {
alert(Geocoder failed due to:+ status);
}
});
});
} else {
alert(地理定位服务不被你的浏览器支持。);
}

< / script>
< / head>
< body>
< / body>
< / html>


I am trying to use HTML 5 GeoLocation to get a longitude and latitude and then use Google Maps API to get the country code of that longitude/latitude. Can anybody tell me where I am going wrong in my code, I currently get the Javascript error 'this.lat is not a function' in main.js :

    <html>

<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=drnhdhddfhgfghfg" type="text/javascript"></script>

<script type="text/javascript">

    if (navigator.geolocation) { 

            navigator.geolocation.getCurrentPosition(function(position) {  

            var lat = position.coords.latitude;
            var lng = position.coords.longitude;

                var latlng = new google.maps.LatLng(lat, lng);

            $.post('http://maps.googleapis.com/maps/api/geocode/json', { latlng: latlng, sensor: false }, function (results) {
                alert(results);
            });
            });




    } 
    else {
            alert("Geolocation services are not supported by your browser.");
    } 

</script>

</head>

<body>



</body>

</html>

解决方案

Try this:

<html>

<head>
<script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script>
<script type="text/javascript">

if (navigator.geolocation) { 

        navigator.geolocation.getCurrentPosition(function(position) {  

        var lat = position.coords.latitude;
        var lng = position.coords.longitude;

        var latlng = new google.maps.LatLng(lat, lng);

    var geocoder = new google.maps.Geocoder();

geocoder.geocode({'latLng': latlng}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    if (results[1]) {
    alert(results[7].formatted_address);
    }
  } else {
    alert("Geocoder failed due to: " + status);
  }
});
      });
      } else {
        alert("Geolocation services are not supported by your browser.");
} 

</script>
</head>
<body>
</body>
</html>

这篇关于从Google地图和HTML 5地理位置获取国家/地区代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆