给定纬度和经度界限后,如何将Google Maps API限制在特定区域? [英] How do I restrict Google Maps API to a specific area when its latitude and longitude bound is given?

查看:59
本文介绍了给定纬度和经度界限后,如何将Google Maps API限制在特定区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在移动屏幕上显示特定区域的google地图.当发生用户交互(例如缩放或移动地图)时,地图不应移动到该区域之外.

I only want to display a google map of a specific area on the mobile screen. The map should not move beyond that area when there is user interaction such as zoom or moving the map.

例如:这些是经纬度范围:

For example: These are latitude and longitude bound:

LatLng one = new LatLng(42.0140555,-88.2131937);
LatLng two = new LatLng(40.993729,-87.6622417); 

推荐答案

我以这种方式找到了解决方案.在 onMapReady()函数上执行此操作.

I have found the solution this way. Do this on onMapReady() function.

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    //get latlong for corners for a specified area of map

    LatLng one = new LatLng(42.0140555,-88.2131937);
    LatLng two = new LatLng(40.993729,-87.6622417); 

    LatLngBounds.Builder builder = new LatLngBounds.Builder();

    //add them to builder
    builder.include(one);
    builder.include(two);

    LatLngBounds bounds = builder.build();

    //get width and height to current display screen
    int width = getResources().getDisplayMetrics().widthPixels;
    int height = getResources().getDisplayMetrics().heightPixels;

    // 20% padding
    int padding = (int) (width * 0.20);

    //set latlong bounds
    mMap.setLatLngBoundsForCameraTarget(bounds);

    //move camera to fill the bound to screen
    mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding));

    //set zoom to level to current so that you won't be able to zoom out viz. move outside bounds
    mMap.setMinZoomPreference(mMap.getCameraPosition().zoom);
}

这篇关于给定纬度和经度界限后,如何将Google Maps API限制在特定区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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