Issue
and do plz tell me the right emulator specification... can it be error of emulator......
I have tried by following links
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
I am trying to build the app which need marker clustered so i have tried by so many times read the Google API create the demo but every time it show error suggest me also that the web application is using map API v3 clustering the marker which API i have to used API v2 or apiv3 of map to build an app please help me out i am newbie at android or give me some link where i can found my solution
.............................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]
Solution
Though I do so much R$D I found for single package name you have to generate single Api key for developing the map in android. though i have done create 2 3 api key for different project with same package name.
Answered By - vaib
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.