- 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
6 comments:
Hi Padawan
It's a very nice tutorial. I am using Android SDK ver 1.1_r1
but the map is not shown. Instead only gray grid lines are shown. I have my android maps API key. I don't know how to check the validity of the key.
regards
Hidayath
Sorry, the link is outdated.
Try.
http://developer.android.com/guide/topics/location/geo/mapkey.html
Great tutorial; however I am having the same problem as Hidayath.
I have a valid maps API Key but I only get the grid lines, suggestions? also, the "http://developer.android.com/guide/topics/location/geo/mapkey.html" link is broken.
Regards,
Will
Sorry about this. The link keeps changing.
And I haven't had time lately to update my posts.
Here's what the new link I found in the android site.
http://code.google.com/android/add-ons/google-apis/mapkey.html
Based from the link, it looks like I have to update this post.
hey i was going through ur tutorial which was really very helpful...but i need some help...i am trying to develop an app to measure small steps...but cant fix my mind on which will be the most efficient way to do it..gps..accelerometer..inbuilt compass..the main aim of the app is to detect the smallest movement and it s direction..can u suggest anything??? ~ Arindam Nath
GPS is unreliable, I don't have experience with accelerometer yet, but I think that is the way to go.
:)
Post a Comment