如何使用map apiv2或map api v3在android中进行markererclusterer

时间:2014-02-18 10:16:41

标签: android google-maps-api-3 android-mapview android-maps android-maps-v2

并且请告诉我正确的模拟器规范...... 这可能是模拟器的错误......

我尝试过以下链接

https://www.google.co.in/?gws_rd=cr&ei=iCf5Upm7KsWXrAedkoCoDg#q=marker+cluster+example+in+android
http://karnshah8890.blogspot.in/2013/04/clustering-on-google-map-v2.html
https://developers.google.com/maps/documentation/android/utility/marker-clustering
https://developers.google.com/maps/documentation/android/utility/marker-clustering
http://stackoverflow.com/questions/7447350/android-maps-point-clustering
http://stackoverflow.com/questions/14204554/google-maps-marker-clusters-in-android
https://github.com/twotoasters/clusterkraf/
https://github.com/nodesagency-mobile/Android-Google-Map-V2-Sample
https://github.com/googlemaps/android-maps-utils/tree/master/demo/src/com/google/maps/android/utils/demo/model
https://github.com/Bersh/MarkersCluster/blob/master/res/menu/activity_main.xml
https://github.com/damianflannery/Polaris/blob/clustering/sample/src/com/cyrilmottier/android/polarissample/util/Config.java
http://umut.tekguc.info/en/content/google-android-map-v2-step-step
http://stackoverflow.com/questions/15495171/cluster-markers-in-google-maps-android-v2/15510054#15510054

我正在尝试构建需要标记聚类的应用程序,所以我已经尝试了很多次阅读Google API创建演示但每次显示错误都表明我也是Web应用程序正在使用map API v3聚类标记哪个API我必须使用API​​ v2或apiv3的地图来构建应用程序请帮助我,我是android的新手 或者给我一些链接,我可以找到我的解决方案

.............................mainactivity class..........................................

    package com.example.cluster;

    import java.util.ArrayList;

    import java.util.LinkedHashMap;
    import java.util.concurrent.ExecutionException;

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Point;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.Menu;

    import com.example.demo.MarkerClusterizer;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.CameraPosition;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;

    public class MainActivity extends FragmentActivity {
         private ArrayList<MarkerOptions> markers = new ArrayList<MarkerOptions>();
            private Bitmap markerImage;
            private float oldZoom = 0;
            private GoogleMap map;
            private static final int INTERVAL = 25;
            private LinkedHashMap<Point, ArrayList<MarkerOptions>> clusters;
            private final double initLat1 = 40.462740;
            private final double initLng1 = 30.039572;
            private final double initLat2 = 48.462740;
            private final double initLng2 = 35.039572;
            private static final int MAP_ZOOM_LEVEL = 4;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);
            markerImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher);

            map= ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
             map.getUiSettings().setMyLocationButtonEnabled(true);
                LatLng position = new LatLng(initLat2, initLng2);
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(position, MAP_ZOOM_LEVEL));
                map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
                    @Override
                    public void onCameraChange(CameraPosition cameraPosition) {
                        if (cameraPosition.zoom != oldZoom) {
                            try {
                                clusters = MarkerClusterizer.clusterMarkers(map, markers, INTERVAL);
                            } catch (ExecutionException e) {
                                e.printStackTrace();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        oldZoom = cameraPosition.zoom;
                    }
                });
                createMarkers(map);
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }


            private void createMarkers(GoogleMap map) {
                double initLat;
                double initLng;

                initLat = initLat1;
                initLng = initLng1;
                for (float i = 0; i < 2; i += 0.2) {
                    LatLng pos = new LatLng(initLat + i, initLng + i);
                    markers.add(new MarkerOptions().position(pos).icon(BitmapDescriptorFactory.fromBitmap(markerImage)));
                }

                initLat = initLat2;
                initLng = initLng2;
                for (float i = 0; i < 2; i += 0.2) {
                    LatLng pos = new LatLng(initLat + i, initLng);
                    markers.add(new MarkerOptions().position(pos).icon(BitmapDescriptorFactory.fromBitmap(markerImage)));
                }
                for (float i = 0; i < 2; i += 0.2) {
                    LatLng pos = new LatLng(initLat, initLng + i);
                    markers.add(new MarkerOptions().position(pos).icon(BitmapDescriptorFactory.fromBitmap(markerImage)));
                }

            }
        }
...............................markerclusterizer class...............................
package com.example.demo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.concurrent.ExecutionException;

import android.graphics.Point;
import android.os.AsyncTask;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.model.MarkerOptions;

public class MarkerClusterizer {
    private static GoogleMap map;
    private static int interval;
    private static final int DEFAULT_INTERVAL = 25;


    public static LinkedHashMap<Point,ArrayList<MarkerOptions>>clusterMarkers(GoogleMap googleMap, ArrayList<MarkerOptions> markers) throws ExecutionException, InterruptedException {
        return clusterMarkers(googleMap, markers, DEFAULT_INTERVAL);
    }
    @SuppressWarnings("unchecked")
    public static LinkedHashMap<Point, ArrayList<MarkerOptions>> clusterMarkers(GoogleMap googleMap, ArrayList<MarkerOptions> markers, int i) throws ExecutionException, InterruptedException {
        map=googleMap;
        interval=i;
        Projection projection=map.getProjection();
        LinkedHashMap<MarkerOptions, Point> points=new LinkedHashMap<MarkerOptions, Point>();
        for(MarkerOptions markerOptions:markers){
            points.put(markerOptions, projection.toScreenLocation(markerOptions.getPosition()));
            markerOptions.title("");

        }
        map.clear();
        CheckMarkersTask checkMarkersTask=new CheckMarkersTask();
        checkMarkersTask.execute(points);



        return checkMarkersTask.get();

    }

    private static class CheckMarkersTask extends AsyncTask<LinkedHashMap<MarkerOptions, Point>, Void, LinkedHashMap<Point, ArrayList<MarkerOptions>>> {

        private double findDistance(float x1, float y1, float x2, float y2) {
            return Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
        }
        @Override
        protected LinkedHashMap<Point, ArrayList<MarkerOptions>> doInBackground(LinkedHashMap<MarkerOptions, Point>... params) {
            LinkedHashMap<Point, ArrayList<MarkerOptions>> clusters = new LinkedHashMap<Point, ArrayList<MarkerOptions>>();
            LinkedHashMap<MarkerOptions, Point> points = params[0];
            for (MarkerOptions markerOptions : points.keySet()) { //go thru all markers
                Point point = points.get(markerOptions);
                double minDistance = -1; //Currently found min distance. This need for finding nearest point.
                Point nearestPoint = null; //Currently found nearest point
                double currentDistance;
                for (Point existingPoint : clusters.keySet()) {  //try to find existing cluster for current marker
                    currentDistance = findDistance(point.x, point.y, existingPoint.x, existingPoint.y);
                    if ((currentDistance <= interval) && ((currentDistance < minDistance) || (minDistance == -1))) {
                        minDistance = currentDistance;
                        nearestPoint = existingPoint;
                    }
                }

                if (nearestPoint != null) {
                    clusters.get(nearestPoint).add(markerOptions);
                } else {
                    ArrayList<MarkerOptions> markersForPoint = new ArrayList<MarkerOptions>();
                    markersForPoint.add(markerOptions);
                    clusters.put(point, markersForPoint);
                }
            }
            return clusters;
        }

        @Override
        protected void onPostExecute(LinkedHashMap<Point, ArrayList<MarkerOptions>> clusters) {
            for (Point point : clusters.keySet()) {
                ArrayList<MarkerOptions> markersForPoint = clusters.get(point);
                MarkerOptions mainMarker = markersForPoint.get(0);
                int clusterSize = markersForPoint.size();
                if (clusterSize > 1) {
                    mainMarker.title(Integer.toString(clusterSize));
                }

                map.addMarker(mainMarker);
            }
        }
    }
    }


  ![..............activity_main.xml...............................................
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <fragment 
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>t
    </RelativeLayout>
..................manifest.xml................................................
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cluster"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <permission
        android:name="com.example.cluster.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.demo.permission.MAPS_RECEIVE" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.cluster.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDqFw-lJjvppil-ixpHjBolINPqJO2b83Q" />
    </application>

</manifest>][1]

2 个答案:

答案 0 :(得分:0)

  

请告诉我正确的模拟器规范

我真的很喜欢GenyMotion来测试我的Android应用程序。它比Android提供的要好得多。

here下载Genymotion,然后添加您想要的模拟器

  

错误消息不支持设备上的opengle

对于真实设备,请确保您的设备支持地图所需的OpenGL ES版本2.

对于标记群集,您可以阅读文档here

答案 1 :(得分:0)

虽然我做了很多R $ D我发现单包名称你必须生成单个Api键来开发android中的地图。 虽然我已经为具有相同包名的不同项目创建了2 3 api密钥。