Google Maps Android API V2简用总结(显示,添加marker,根据两个marker自动缩放)

1. 安装开发环境,参照Google Maps Android API v2 的Getting Started,具体不详述了,程序跑起来,能看到google地图就行。

2. 添加marker:

         mMap.addMarker(new MarkerOptions().position(new LatLng(10, 10)).title("Marker"));即可添加一个简单的marker,关于marker更多的内容可以查看Markers

         可以将上述调用的返回值保存起来,Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(10, 10)).title("Marker"));作为当前位置的标识,随着经纬度的改变,调用marker.position(new LatLng(latitude, longitude));就会改变marker的位置。如果需要以当前位置显示在地图的中心点,调用mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));,更多内容请查看Moving the camera.

3. 根据两点自动缩放Position1(latitude1, longitude1), Position2(latitude2, longitude2):

         3.1 只需要两个点都显示出来:
                LatLngBounds center = new LatLngBounds(new LatLng(latitude1<latitude2?latitude1:latitude2, longitude1<longitude2?longitude1:longitude2), new LatLng(latitude1>latitude2?latitude1:latitude2, longitude1>longitude2?longitude1:longitude2));
                mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(center, 0));注意LatLngBounds的构造函数,第一个LatLng的参数为(south,west),第二个LatLng的参数为(north,east)

         3.2 Position1显示在中心点,同时显示Position2:

                先计算Position1和Position2的水平(经度差horizontal)和垂直(纬度差vertical)方向的的差值,center = new LatLngBounds(new LatLng(latitude1-vertical, longitude1-horizontal), new LatLng(latitude1+vertical, longitude1+horizontal));

4. 如果出现

java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view.  Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.
是因为地图还没有加载完成,就开始change view,可以在地图加载完成在change:
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded()
                    {
                        mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(center, 0));
                    }
                });

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值