简体   繁体   中英

how to restrict maps to only one country using google maps api for android

I am totally new to google's maps api for android and I was wondering if it is possible to restrict the area the map is showing to one country only. I mean my target users are in my own country only and it is not useful nor logical to show them the whole world when the application logic is going to be only on this country.

Any ideas?

ps: if it is not possible in google's api,is there a way to do this with another api form another source which provides a mapping service?

If you are still interested, now it's possible to restrict the user's panning to a given area using the setLatLngBoundsForCameraTarget method.

You will need to update the dependencies for your Play Services:

dependencies {
    compile 'com.google.android.gms:play-services:9.6.1'
}

I am pretty sure you cannot only display your chosen country using the google api service. However, what you can do, is restrict google places auto complete to a specific country, by finding your country code (eg using this link https://countrycode.org/ ) and then adding the query to your google places api eg

components=country:your_country_code    //Insert code into example

For example, this is what is would look like:

  https://maps.googleapis.com/maps/api/place/autocomplete/json?
input=bhubaneshwar&components=country:in&sensor=false&key=your_key

out of the box this is not possible. However what you could theoretically do it find the max bounding box that shows the country in question then everytime the camera moves check to make sure the bounding box is inside the max one. If it is not set it to the max. that would restrict them to a certain spot

you can override the map and specific the latitude and longitude and zoom in with suitable for you and disable the gesture of map see this example it is answered before

limit scrolling and zooming Google Maps Android API v2

You can restrict the user view to specific square -maybe- of the map, to do so, you can use the following:

private GoogleMap mMap;
// Create a LatLngBounds that includes the city of Adelaide in Australia.
private LatLngBounds ADELAIDE = new LatLngBounds(
  new LatLng(-35.0, 138.58), new LatLng(-34.9, 138.61));
// Constrain the camera target to the Adelaide bounds.
mMap.setLatLngBoundsForCameraTarget(ADELAIDE);

in this case, the user will not be able to zoom in, in the map in areas where the camera is totally out of the range -square- you specified.

for more details, source: https://developers.google.com/maps/documentation/android-api/views#restricting_the_users_panning_to_a_given_area

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM