如何使用Android上视口标记管理 [英] How to use Viewport Marker Management on Android

查看:209
本文介绍了如何使用Android上视口标记管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到如何使用此techniche在Android上的任何完整的解释,所以我决定创建一个Q&放大器;一个线程

I havent found any complete explanation on how to use this techniche on Android so i decided to create a Q&A thread.

如果您的应用程序已经显示出大量的标记在谷歌地图和集群他们是不够的,prevent从工作您的应用程序太慢了,再一个你最好的选择就是用这个视口标记管理techniche 。你可以在这里阅读中的理论解释: https://developers.google.com/maps/articles/toomanymarkers

If your app has to show a large amount of markers on a google map and clustering them is not enough to prevent your app from working too slow, then one of your best choices is to use this Viewport Marker Management techniche. You can read the theorical explanation here: https://developers.google.com/maps/articles/toomanymarkers

我写了下面的简短说明...

I wrote a short guide below...

推荐答案

1°---如果创建地图,你必须设置OnCameraChangeListener,让你的屏幕范围像这样的活动:

1°--- In the activity where map is created you have to set the OnCameraChangeListener and get the bounds of your screen like this:

mMap.setOnCameraChangeListener(new OnCameraChangeListener() {

        @Override
        public void onCameraChange(CameraPosition arg0) {

        LatLngBounds bounds = mapa.getProjection().getVisibleRegion().latLngBounds;

        }

2°---这取决于你如何获取指标数据这一步骤可能会有所不同。基本上,你所要做的是计算,如果lat和长你的每一个标记在屏幕范围内。我会告诉你如何通过获取标记表里面从一个SQLite数据库中存储latitud两种不同的双clomuns数据和纬度来做到这一点。

2°--- This step may vary depending how you fetch the markers data. Basically, what you have to do is to calculate if the lat and long of each of your markers are inside the screen bounds. I will show you how to do it by fetching the data from a SQLite data base storing latitud and longitude in two different DOUBLE clomuns inside the markers table.

mMap.setOnCameraChangeListener(new OnCameraChangeListener() {

        @Override
        public void onCameraChange(CameraPosition arg0) {

        LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;

        LatLng northeast = bounds.northeast;
        String boundLat = String.valueOf(northeast.latitude);
        String boundLong = String.valueOf(northeast.longitude);

        LatLng southwest = bounds.southwest;

        String boundLat2 = String.valueOf(southwest.latitude);
        String boundLong2 = String.valueOf(southwest.longitude);

        //Remove all markers from map

        mMap.clear(); // or if your a using cluster manager:
        //mClusterManager.clearItems();

        String[] fields = new String[] { "name", "latitude", "longitude" };
        String[] args = new String[] {boundLat, boundLong, boundLat2, boundLong2,};

        Cursor markers = dataBase.query("markers", fields, "latitude<=? AND longitude<=? AND latitude>=? AND longitude>=?");

        if (markers.moveToFirst()) {
                do {
                    mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(marker.getDouble(1), marker.getDouble(2)))
                    .title(marker.getString(0)) );

                     // or if you are using cluster manager create and add the items as you normaly do.

                   } while (c.moveToNext());

                   //if using cluster manager add :
                   //mClusterManager.cluster();
                   }
    }
});

我们的想法是pretty的方便,只需记住,你的标记纬度和隆基必须比你的屏幕的东北位置比西南角小,大,或者仅使用LatLngBounds.contains功能。

The idea is pretty easy, just have in mind that your markers lat and longi have to be smaller than the northeast position of your screen and bigger than the southwest corner, or just use the LatLngBounds.contains function.

编辑:

要避免一个标记,这是不是已经在屏幕中心时,点击信息窗口得到封闭,你可以改变的标志点击监听器的默认操作,删除,移动摄像机。

To avoid InfoWindow getting closed when clicking on a marker which is not already in the center of the screen, you can change the marker click listener default action, removing the camera move.

map.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker arg0) {
            arg0.showInfoWindow();

            return true; //must be true, if not, it will execute the default code after yours
        }

    });

这篇关于如何使用Android上视口标记管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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