views:

23

answers:

2

hi, when i am trying to run the example of Maps as in http://developer.android.com/resources/tutorials/views/hello-mapview.html , the menu button doesn't seem to work. I tried the default Maps app in the emulator and there it was working. Also i intend to add an item when a user presses that button , how can do it?

+1  A: 

That is the Example of Mapview not for a Menus. Look out this Documentation. It had the answer of all your Questions.

Praveen Chandrasekaran
A: 

i tried this but still the menu button isn't working the MapActivity class

package com.example.MyMapActivity;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
 import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
 import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MyMapActivity extends MapActivity {

private MapView mapview;
private MapController mapcontroller;
private GeoPoint gp;


//creating a class to for OverLays


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //setting the zoom controls
    mapview = (MapView)findViewById(R.id.mapview);
    mapview.setBuiltInZoomControls(true);
    mapview.setSatellite(true);

    List<Overlay>mapOverlays = mapview.getOverlays();
    // pass the drawable to instance of the OVerlay class.
    Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
    MyOverlay overlay = new MyOverlay(drawable,this);


   //setting a GeoPoint
    mapcontroller=mapview.getController();
    String coordinates[] = {"23.68855","86.97455"};
    double lat = Double.parseDouble(coordinates[0]);
    double longi = Double.parseDouble(coordinates[1]);
    gp = new GeoPoint((int)(lat*1E6), (int)(longi*1E6));

    OverlayItem overlayitem = new OverlayItem(gp, "Hello ", "I'm in Asansol");

    GeoPoint point2 = new GeoPoint(35410000, 139460000);
    OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");

    overlay.addOverlay(overlayitem); //  call this method for each overlayitem 
    overlay.addOverlay(overlayitem2);
    mapOverlays.add(overlay);
    }
// to inflate the menu defined in the map_menu xml file    
public boolean OnCreateOptionsMenu(Menu menu)
 {
   MenuInflater  inflator = getMenuInflater();
   inflator.inflate(R.menu.map_menu, menu);
   return true;
}

public boolean OnOptionsItemSelected(MenuItem item)
{
   switch(item.getItemId())
   {
   case R.id.satellite_view:
       mapview.setSatellite(true);
       return true;
   case R.id.street_view:
       mapview.setStreetView(true);
           return true;
    default:
        return super.onOptionsItemSelected(item);
   }
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    // this method must return true if your app returns driving directions , else return false
    return false;
}

}

the res/menu/map_menu.xml file:

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"&gt;
<item android:id="@+id/satellite_view"
    android:icon="@drawable/satellite_view"
    android:title="@string/satellite_view"/>

<item android:id="@+id/street_view"
    android:icon="@drawable/street_view"
    android:title="@string/street_view"/></menu>
pranay