简体   繁体   English

在适用于Android的Google Maps API v2中保留对地图更新的以前的缩放值

[英]Retaining previous zoom value on a Map update in Google Maps API v2 for Android

I'm working on a map based application. 我正在开发基于地图的应用程序。 What it does is receives data from servers for the latitude and longitude. 它所做的是从服务器接收纬度和经度的数据。 It refreshes data every x seconds. 它每x秒刷新一次数据。 It then moves a marker on the map to the new location(the changed one) that was received. 然后,它将地图上的标记移动到收到的新位置(更改的位置)。

My problem is, while using the map, I don't want the zoom level to be reset for the marker. 我的问题是,在使用地图时,我不希望为标记重置缩放级别。 I want it to retain the zoom level that the user may have adjusted to while looking at the map. 我希望它保留用户在查看地图时可能已调整到的缩放级别。

I have a marker named bus defined as an instance variable: 我有一个名为bus的标记定义为实例变量:

  Marker bus = null;

Here is the code for my BroadCast Receiver that recieves an intent that the data has been refreshed. 这是我的BroadCast接收器的代码,该代码表明已刷新数据。

    private BroadcastReceiver myReceiver = new BroadcastReceiver() {

        @Override
        //public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {

               if(bus!= null){
                   bus.remove();
               }

            bun = ((TabActivity)Tab2Fragment.this.getActivity()).getDataFromActivity();
            try{
            BusLocation = new LatLng(Double.parseDouble(bun.getString("lat")),Double.parseDouble(bun.getString("long")));
            }catch(JSONException e){
                e.printStackTrace();
            }

            bus = map.addMarker(new MarkerOptions().position(BusLocation)
                    .title("Bus is Here")
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.ic_launcher)));

                // Move the camera instantly to the Bus Location with a zoom of 15.


map.animateCamera(CameraUpdateFactory.newLatLngZoom(BusLocation, 15), 1000, null);

        }
    };

Every time it updates, it re-zooms the level. 每次更新时,都会重新缩放级别。 How do I not rezoom the level? 我如何不缩放水平? I tried using newLatLng() instead of LatLngZoom, but then on initialization, it is zoomed extremely far away. 我尝试使用newLatLng()代替LatLngZoom,但是在初始化时,它的缩放距离非常远。 And I won't have data avaiable to zoom it in with relevance. 而且我没有可用的数据来放大相关性。

Thanks for any help :) 谢谢你的帮助 :)

Turns out, its way too simple. 事实证明,它的方法太简单了。 I wasn't thinking. 我没想 For what it's worth: 物有所值:

Have a boolean value 具有布尔值

private boolean first = true;

Then in the update: 然后在更新中:

            if(first)
            {
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(BusLocation, 15), 1000, null);
                first = false;
            }
            else{
                map.animateCamera(CameraUpdateFactory.newLatLng(BusLocation), 1000, null);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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