I am completely new to the android however coming across this I hit a few problems with the code being out dated and also the functions being a little lacking although they are probably lacking as it was a tutorial so for my application and personal testing Adam (my co-developer) and I created this for just testing the map API and are now using it as a platform for developing apps with the map api. The original post still has relevant information however hopefully this helps people out with the obvious changes that have outdated that code and made it non runnable.
maptest.java
Using java Syntax Highlighting
- package com.example.maptest;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import com.google.android.maps.GeoPoint;
- import com.google.android.maps.MapActivity;
- import com.google.android.maps.MapView;
- public class maptest extends MapActivity {
- private MapView myMapView;
- private GeoPoint p;
- //map move increment
- double MAP_MOVE_INCREMENT = 0.25;
- //location of statue of liberty
- int pLat = (int) (40.689213 * 1000000);
- int pLong = (int) (-74.044558 * 1000000);
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- myMapView = new MapView(this, "YOUR API KEY GOES HERE");
- //create the controller and define some initial settings for the view
- myMapView.getController().setZoom(20);
- myMapView.setSatellite(true);
- //move the map
- mapLocationUpdate();
- // Make myMapView the exilicit view of this app
- setContentView(myMapView);
- }
- public boolean onKeyDown(int keyCode, KeyEvent event){
- switch (keyCode){
- case KeyEvent.KEYCODE_I:
- myMapView.getController().zoomIn(); //zoom in
- return true;
- case KeyEvent.KEYCODE_O:
- myMapView.getController().zoomOut(); //zoom out
- return true;
- case KeyEvent.KEYCODE_S:
- myMapView.setSatellite(satelliteToggle());
- return true;
- case KeyEvent.KEYCODE_DPAD_UP:
- mapPan(0);//move north
- return true;
- case KeyEvent.KEYCODE_DPAD_DOWN:
- mapPan(1);//move south
- return true;
- case KeyEvent.KEYCODE_DPAD_LEFT:
- mapPan(2);//move west
- return true;
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- mapPan(3);//move east
- return true;
- }
- return false;
- }
- //direction is either 0, 1, 2, 3 relating to N S W E
- private void mapPan(int direction){
- switch(direction){
- case 0://north
- pLat = (int) (p.getLatitudeE6() + (myMapView.getLatitudeSpan()*MAP_MOVE_INCREMENT));
- break;
- case 1://south
- pLat = (int) (p.getLatitudeE6() - (myMapView.getLatitudeSpan()*MAP_MOVE_INCREMENT));
- break;
- case 2://west
- pLong = (int) (p.getLongitudeE6() - (myMapView.getLongitudeSpan()*MAP_MOVE_INCREMENT));
- break;
- case 3://east
- pLong = (int) (p.getLongitudeE6() + (myMapView.getLongitudeSpan()*MAP_MOVE_INCREMENT));
- break;
- }
- mapLocationUpdate();
- }
- private void mapLocationUpdate(){
- p = new GeoPoint (pLat, pLong);
- myMapView.getController().setCenter(p);
- //if you prefer animations you can actually use the below line to move the map instead of .setCenter
- //myMapView.getController().animateTo(p);
- }
- private boolean satelliteToggle(){
- //if the satellite view is on, this will return false. Oppisite of course if the satellite view is off it will return true
- if(myMapView.isSatellite()){
- return false;
- }
- return true;
- }
- protected boolean isRouteDisplayed() {
- // TODO Auto-generated method stub
- return false;
- }
- }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
main.xml
Using java Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
AndroidManifest.xml
Using java Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.maptest"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-permission android:name="android.permission.INTERNET" />
- <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
- <uses-library android:name="com.google.android.maps" />
- <activity android:name=".maptest"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- </manifest>
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


