Activity程式碼如下:
package com.example.aa;
import java.util.List;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
public class MainActivity extends MapActivity {
private MapView mapView;
private MapController controller;
private MyLocationOverlay locationOverlay;
private LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
private void init() {
mapView = (MapView) findViewById(R.id.MAP);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
controller = mapView.getController();
controller.setZoom(14);
List<Overlay> overlays = mapView.getOverlays();
locationOverlay = new MyLocationOverlay(this, mapView);
overlays.add(locationOverlay);
locationOverlay.enableCompass();
locationOverlay.enableMyLocation();
locationOverlay.runOnFirstFix(new Runnable() {
public void run() {
controller.animateTo(locationOverlay.getMyLocation());
}
});
}
}
Layout.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.maps.MapView
android:id="@+id/MAP"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0PAijieTDH_kcO7YM_OERwTzP2RO7uqipai0osQ"
android:clickable="true"/>
</RelativeLayout>
AndroidManifest.xml如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa"
android:versionCode="1"
android:versionName="1.0" android:installLocation="preferExternal">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" android:required="true"/>
</application>
</manifest>
參考文章:
http://www.dotblogs.com.tw/alonstar/archive/2011/05/26/26282.aspx