May 21, 2012

Switch Activity once a marker on a MapView tapped

This example continuously work on the article "Handle both onTap(GeoPoint p, MapView mapView) and onTap(int index) implemented in MapView": When a marker on the MapView tapped, it will switch to another Activity with some data passes; Title, Latitude and Longitude.

Switch Activity once a marker on a MapView tapped


Create a new Android project target with Google APIs.

In order to use MapView on your app, you have to obtain your Map API Key, refer http://android-coding.blogspot.com/2011/06/mapview-and-maps-api-key.html

The main layout, main.xml. You have to replace your own Map API Key in com.google.android.maps.MapView. (It's same as before in "Handle both onTap(GeoPoint p, MapView mapView) and onTap(int index) implemented in MapView")
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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="--- Your own Map API Key ---" />
 
</LinearLayout>

The main part to be changed is in onTap(int index) method of MyItemizedOverlay.java
package com.AndroidMapView;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
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
 public boolean onTap(GeoPoint p, MapView mapView) {
  // TODO Auto-generated method stub
  if(super.onTap(p, mapView)){
   return true; 
  }
     
  String title = "pt:" + String.valueOf(overlayItemList.size() + 1);
  String snippet = "geo:\n"
    + String.valueOf(p.getLatitudeE6()) + "\n"
    + String.valueOf(p.getLongitudeE6());
     
  addItem(p, title, snippet);
     
  return true;
 }

 @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; 
  
  Intent intent=new Intent(context, NewActivity.class);
  intent.putExtra("Title", overlayItemList.get(index).getTitle());
  intent.putExtra("LatE6", overlayItemList.get(index).getPoint().getLatitudeE6());
  intent.putExtra("LonE6", overlayItemList.get(index).getPoint().getLongitudeE6());
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(intent);
  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); 
 }
}

The main Activity, AndroidMapViewActivity.java (same as before)
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; 
 }
}

NewActivity.java, the new activity to be opened once marker tapped. It simple retrieve the passed data and display it.
package com.AndroidMapView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class NewActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  TextView textView = new TextView(this);
  setContentView(textView);
  
  Bundle bundle = this.getIntent().getExtras();
  textView.setText(
    bundle.getCharSequence("Title") + "@\n"
    + bundle.getInt("LatE6") + " : " + bundle.getInt("LonE6"));
     
 }

}

AndroidManifest.xml. You have to add the <activity> of NewActivity. You also have to include uses-library of "com.google.android.maps", and uses-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="10" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        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>
        <activity
            android:name=".NewActivity"
            android:label="New" >
        </activity>
    </application>

</manifest>


1 comment:

  1. I need to get the latitude and longitude values of each added point to map (those added by touch not static points) and use them in my math formulas (calculating distance and whatnot), should I create a new array for lat and long or what is the easiest way to achieve this?

    ReplyDelete

Infolinks In Text Ads