The option items is added to the option menu in the method onCreateOptionsMenu(), in which initialize the contents of the Activity's standard options menu.
Because it's only one item in the menu, Menu.add(CharSequence title) is used to add item to the menu. Otherwise, Menu.add(int groupId, int itemId, int order, CharSequence title) or Menu.add(int groupId, int itemId, int order, int titleRes) have to be used.
onOptionsItemSelected() will be called whenever an item in options menu is selected. Also, because it's only one item in the menu, so there are no need to check which item have been selected, otherwise, you have to check the itemId.
Add the following three method in the class AndroidMapView:
public boolean onCreateOptionsMenu(Menu menu) { menu.add("Help"); return super.onCreateOptionsMenu(menu); }
private void openOptionsHelpDialog() { new AlertDialog.Builder(this) .setTitle(R.string.help_title).setMessage(R.string.help_message) .setPositiveButton(R.string.help_ok, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub
} } ) .show(); }
@Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub super.onOptionsItemSelected(item); openOptionsHelpDialog(); return true; }
Implement a file helpmessage.xml in the folder /res/values/, it's used to hold the text used inside the Help Dialog.
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="help_title">Help!</string> <string name="help_message">\n Pan on the Map: touch the screen and move.\n Update location: touch on the screen.\n Zoom: slice on the SeekBar on bottom.\n </string> <string name="help_ok">OK</string> </resources>
layoutopt is a command-line tool that helps you optimize the layouts and layout hierarchies of your applications. You can run it against your layout files or resource directories to quickly check for inefficiencies or other types of problems that could be affecting the performance of your application.
In the last exercise, Move the marker on MapView, user touch on screen to pan the Map and update marker. It's very confuse with the BuiltInZoomControls function. So I separate the zoom function outside the MapView in this exercise. It's a SeekBar under the MapView, user can change the zoom level by sliding on the SeekBar.
It involve modification on mymapview.xml and AndroidMapView.java.
public InterestingLocations(Drawable defaultMarker, int LatitudeE6, int LongitudeE6) { super(defaultMarker); // TODO Auto-generated constructor stub this.marker=defaultMarker; // create locations of interest GeoPoint myPlace = new GeoPoint(LatitudeE6,LongitudeE6); myOverlayItem = new OverlayItem(myPlace, "My Place", "My Place"); locations.add(myOverlayItem);
In the last exercises, Display a marker on MapView, using Overlays and Get center location of a map, using MapView.getMapCenter(), a marker is displayed on the center of the MapView. In this exercise, I want to add the capability of moving the marker on the Map. Unfortunately, I can't find any solution (from books and internet) to move (drag) the marker on MapView, similar to web version. Finally, I made a alternative.
If user Touch on screen without moving, it will be treated as update marker, the marker will be place on the new location, and it will be centered on the MapView. If user Touch on screen and Move, it will be treated as pan the MapView, the marker will not be changed.
The main logic is in the method onTouchEvent(MotionEvent arg0, MapView arg1), ACTION_UP, ACTION_DOWN and ACTION_MOVE cases.
((int)arg0.getX(), (int)arg0.getY()) is the on-screen pixel coordinates, it can be translated to latitude/longitude(GePoint) using Projection.fromPixels.
public InterestingLocations(Drawable defaultMarker, int LatitudeE6, int LongitudeE6) { super(defaultMarker); // TODO Auto-generated constructor stub this.marker=defaultMarker; // create locations of interest GeoPoint myPlace = new GeoPoint(LatitudeE6,LongitudeE6); myOverlayItem = new OverlayItem(myPlace, "My Place", "My Place"); locations.add(myOverlayItem);
MOTODEV Studio for Android is a Complete Development Package: One installer ensures an integrated development environment with Eclipse 3.5 and Android Development Tools (ADT) plus automatic download and configuration of the latest Android SDK.
MOTODEV Studio for Android Version 1.0.2 now support Android 1.1, Android 1.5, Android 1.6, Android 2.0 (Motorola Droid and Milestone)
Android 2.0.1 is a minor platform release deployable to Android-powered handsets starting in December 2009. This release includes minor API changes, bug fixes and framework behavioral changes.
In the last exercise, Display a marker on MapView, using Overlays, a marker will be displayed in Mode 1 (with starting location) only. In this exercise, current center location of the Map in Mode 0 (without starting location) will be retrieved using the method MapView.getMapCenter(). Such that the appearance of both mode will be kept consistance.
To achieve it, AndroidMapView have to be modified, to add function in onCreate() to handle Mode 0.
private CheckBox.OnClickListener mySatelliteOnClickListener = new CheckBox.OnClickListener(){
public void onClick(View v) { // TODO Auto-generated method stub SetSatellite(); } };
class InterestingLocations extends ItemizedOverlay<OverlayItem>{
private List<OverlayItem> locations = new ArrayList<OverlayItem>(); private Drawable marker;
public InterestingLocations(Drawable defaultMarker, int LatitudeE6, int LongitudeE6) { super(defaultMarker); // TODO Auto-generated constructor stub this.marker=defaultMarker; // create locations of interest GeoPoint myPlace = new GeoPoint(LatitudeE6,LongitudeE6); locations.add(new OverlayItem(myPlace , "My Place", "My Place")); populate(); }