如何在中国访问Google Maps API

戈皮·纳斯

我正在使用google maps api获取我的IBM Mobilefirst项目中的用户位置,并且除中国以外的所有国家/地区都按预期工作正常。我知道这是因为中国已禁止在其国家/地区访问google api。我可以使该应用程序即使在中国也可以使用。我提供了以下代码片段供参考。

这是我头脑中的脚本

 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBaKx9iQrPQuOmAc2DkMRgjFdT0_XTdbmE&sensor=false&v=3&libraries=geometry"></script>

JavaScript代码

function getLocation() {
    busy = new WL.BusyIndicator ();
    busy.show();
    if (navigator.geolocation) {

        navigator.geolocation.getCurrentPosition(showPosition, showError,options);
    } else { 
        alert( "Geolocation is not supported");}
    }

function showPosition(pos) {

    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);
    latitude=pos.coords.latitude;
    longitude=pos.coords.longitude;
    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var addresscomponent=results[0].address_components;
            var addresslength=addresscomponent.length;
            for(var i=0;i<addresslength;i++){
                if(addresscomponent[i].types[0]=='country'){
                    countryname=addresscomponent[i].short_name;
                }
                else if(addresscomponent[i].types[0]=='locality'){
                    cityname=addresscomponent[i].short_name;
                }
            }
}

function showError(error) {
    alert(error);
    busy.hide(); 
       }
阿南德·辛格(Anand Singh)

为什么我不能从中国访问Google Maps API?

Google Maps API在中国境内通过maps.google.cn域提供。此域不支持https。从中国向Google Maps API提出请求时,请将https://maps.googleapis.com替换http://maps.google.cn

例如:

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA

会成为:

http://maps.google.cn/maps/api/geocode/json?address=1600+露天剧场+百汇,+山景+景观,+ CA

参考:Google常见问题解答

更新(
针对国家/地区)使用此方法:

$.getJSON("http://ip-api.com/json/", function (data) {
        var country = data.country_name;//your country
        alert(country);
        
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Maps API RefererNotAllowedError

来自分类Dev

Google Maps API DeletedApiProjectMapError

来自分类Dev

Google Maps Api许可

来自分类Dev

Google Maps Api

来自分类Dev

Google Maps REST API

来自分类Dev

Google Maps API请求

来自分类Dev

Google Maps Directions API

来自分类Dev

Google Maps API参数

来自分类Dev

angular-google-maps,访问常规的Google Maps API

来自分类Dev

如何使用React Google Maps API访问Map对象?

来自分类Dev

Google Maps API访问权限发布

来自分类Dev

如何在Google Maps API上绘制透明圆圈

来自分类Dev

如何在Google Maps API中使用Java?

来自分类Dev

如何在Google Maps API中找出该区域

来自分类Dev

如何在Google Maps API上的标记周围添加圆圈

来自分类Dev

如何在Google Maps JS API中制作虚线的折线?

来自分类Dev

如何在for循环中处理Google Maps API调用

来自分类Dev

如何在Google Maps Directions API响应上查找坐标?

来自分类Dev

如何在Android Studio中设置Google Maps Utility Api?

来自分类Dev

如何在Google Maps API上的标记周围添加圆圈

来自分类Dev

如何在Google Maps API上绘制透明圆圈

来自分类Dev

最终如何在Google Maps API中更改标记位置?

来自分类Dev

Google Maps Javascript API与Google Maps Embed API

来自分类Dev

缺少Google Maps RouteBoxer API

来自分类Dev

异步加载Google Maps API

来自分类Dev

动态加载Google Maps API

来自分类Dev

Google Maps API的JavaScript框架

来自分类Dev

Google Maps API搜索限制

来自分类Dev

Google Maps Api机场搜索