Detect touch on marker in MapView

By overriding of the onTap(int index) callback method of ItemizedOverlay extended class, we can detect the touched item with index.

Detect touch on marker in MapView

implement MyItemizedOverlay.java class extends ItemizedOverlay
package com.AndroidMapView;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
Context context;

public MyItemizedOverlay(Drawable marker, Context c) {
super(boundCenterBottom(marker));
// TODO Auto-generated constructor stub
populate();
context = c;
}

@Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
//return super.onTap(index);

Toast.makeText(context,
"Touch on marker: \n" + overlayItemList.get(index).getTitle(),
Toast.LENGTH_LONG).show();

return true;
}

public void addItem(GeoPoint p, String title, String snippet){
OverlayItem newItem = new OverlayItem(p, title, snippet);
overlayItemList.add(newItem);
populate();
}

@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return overlayItemList.get(i);
}

@Override
public int size() {
// TODO Auto-generated method stub
return overlayItemList.size();
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
}
}


main code AndroidMapViewActivity.java
package com.AndroidMapView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class AndroidMapViewActivity extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);

Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);


MyItemizedOverlay myItemizedOverlay
= new MyItemizedOverlay(marker, AndroidMapViewActivity.this);
mapView.getOverlays().add(myItemizedOverlay);

GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
GeoPoint myPoint2 = new GeoPoint(50*1000000, 50*1000000);
myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}


layout, main.xml. Please note that you have to insert your own Map API Key.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="--- Insert your own MAP API Key here ---" />

</LinearLayout>


AndroidManifest.xml, to include uses-library of "com.google.android.maps" and permission of "android.permission.INTERNET".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidMapView"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".AndroidMapViewActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>


next post:
- Detect touch on MapView, onTap(GeoPoint p, MapView mapView)
- Handle both onTap(GeoPoint p, MapView mapView) and onTap(int index) implemented in MapView

0 Response to "Detect touch on marker in MapView"

Posting Komentar