我正在使用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();
}
为什么我不能从中国访问Google Maps API?
Google Maps API在中国境内通过maps.google.cn域提供。此域不支持https。从中国向Google Maps API提出请求时,请将https://maps.googleapis.com替换为http://maps.google.cn。
例如:
会成为:
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] 删除。
我来说两句