- Create MapViewer class that extends MapActivity
package javapadawan.android;
import android.os.Bundle;
import android.view.KeyEvent;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MapViewer extends MapActivity {
private MapView map;
private MapController mc;
private static int zoomValue = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView) findViewById(R.id.map);
mc = map.getController();
mc.setZoom(zoomValue);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
zoomValue = zoomValue + 1;
mc.setZoom(zoomValue);
} else if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
zoomValue = zoomValue - 1;
mc.setZoom(zoomValue);
} else if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
map.setSatellite(false);
} else if(event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
map.setSatellite(true);
}
return super.dispatchKeyEvent(event);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
2. Modify main.xml
3. Set the required permissions in the AndroidManifest.xml
4. Clean, Build and Run the Project. You will see an Empty Map Grid instead of a google map.
4. The reason why the code does not work is that the apiKey needs to be a valid key from google. Below are the steps to obtain a valid key.
- Obtaining a Maps API Key
- Edit the main.xml and replace apiKey with the value from google. See below on how to generate an MD5 Certificate fingerprint.
5. Now clean, build and compile again. Click the UP/Down to ZOOM, and LEFT/RIGHT to switch Map View and Satellite View.
6. Now that I got Map View to work, maybe I'll create something useful next time.
Source Code