How to access google maps api in China

2024/2/27 7:15:10

I am using google maps api to get user location in my IBM Mobilefirst project and it is working fine as expected in all the countries except china.I know this is because china has blocked access to google apis in their country.Is there any workaround that I can make so that the application works even in china. I have given the code snippet below for reference.

This is the script I have in head

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

javascript code

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(); }
Answer

Why can't I access Google Maps APIs from China?

The Google Maps APIs are served within China from the domain maps.google.cn. This domain does not support https. When making requests to the Google Maps APIs from China, please replace https://maps.googleapis.com with http://maps.google.cn.

For example:

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

would become:

http://maps.google.cn/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA

reference :Google FAQ

Update
For country use this:

$.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>

http://en.ppmy.cn/q/42155.html

Related Q&A

dijit.form.filteringselect dynamically change options

I have a dijit.form.FilteringSelect component and I want to change the options dynamically. But I get the store from the dijit.form.FilteringSelectwith its store property; there is no setter function i…

html5 video redisplay poster image onMouseOut

How can I stop a html5 video playback, and revert to poster image? Until this.play() is called, the poster image is showing properly.My code:<video loop="loop" onMouseOver="this.play…

What happened to generateLegend() in chartjs 3.0?

My understanding is that it has been depreciated in version 3.0. Is there a alternate method to easily accomplish customization of the legend?

How to use react hooks on react-native with react-navigation

This is App.js using react-navigation. There are two screens on it called HomeScreen and AddScreen. import * as React from react; import { NavigationContainer } from @react-navigation/native; import {…

Is-there an opposite of the `race` operator in RxJS?

I have two observables and I want listen to the one that emits its first value last, is there an operator for this ? Something like that :let obs1 = Rx.Observable.timer(500,500); let obs2 = Rx.Observa…

Making a random number thats a multiple of 10

Im looking to create a random number between two ranges that is a multiple of 10.For example, if I fed the function the parameters 0, 100 it would return one of these numbers:0, 10, 20, 30, 40, 50, 60,…

Canvas redraws only after loop ends

I have an issue with drawing on canvas in a loop.What I want to achieve is that in each loop the script waits for a few milliseconds, then draws on a canvas, the user can actually see the change, and t…

jQuery : Use an alternative to .toggle() which is deprecated

I have some images with class name ".mute_btn" and when i click on them, my images source is changing :$(.mute_btn).toggle(function () {var clicked = false;var src = $(this).attr("src&qu…

Any way to hide CSS and JavaScript file from the client-side user?

Is there any way to hide the CSS and JavaScript file from the client-side user and still embed it in the webpage shown to the user?

How to close an iframe?

After opening an iframe, I sometimes need to close it. Thus far, I just remove the iframe from its parent node, thus cutting it off from the DOM. However, Im finding that this doesnt immediately end …