1 main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <!-- 使用第三方包,不使用本工程包 --> <com.google.android.maps.MapView android:id="@+id/mapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0JJOMTfXnoZBpJ7zmZmlaKSHji-LbJbGz91ew"/></LinearLayout>
2 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cjf.map" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.INTERNET"/><!-- 需要使用網路 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><!-- GPS --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><!-- NET -->" <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="com.google.android.maps"/><!-- 需要添加第三方庫 --> <activity android:name=".MainActivity" 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></manifest>
3, MainActivity
package com.cjf.map;import java.util.List;import com.google.android.maps.GeoPoint;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;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Point;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.Toast;/***繼承MapActivity抽象類別,此類也是繼承自Activity*LocationManager需要監聽地理位置資訊的改變,由於多次需要用到此介面LocationListener,所以直接實現**/public class MainActivity extends MapActivity implements LocationListener{private MapView mapView;private MapController mMapController;private GeoPoint mGeoPoint;private LocationManager mLocationManager ;private Location mLocation; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView) findViewById(R.id.mapView); /** * 1,地圖的載入 */ mapView.setBuiltInZoomControls(true);//支援縮放 mapView.setClickable(true);//設定此方法可以和使用者互動 mapView.setKeepScreenOn(true); mapView.setLongClickable(true); mapView.setStreetView(true);//設定為街景模式 /** * 2,加入設定位置功能 */ mMapController = mapView.getController(); mMapController.setZoom(18);//設定地圖的放大層級 /** * 3,給定經緯度,顯示成都 */ updateMapShow(30.659259,104.065765); /** * 4,在地圖上給這個地點加上一個mark,標籤(注意和地圖不是同一層) */ mapView.getOverlays().add(new MyOverlay()); /** * 5,實現定位功能 */ mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //1,得到我們位置資訊 mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);//使用GPS,許可權fine if(mLocation != null){ //2,更新我們的位置資訊 updateMapShow(mLocation.getLatitude(),mLocation.getLongitude()); }else { mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);//使用網路,許可權corase if(mLocation != null) updateMapShow(mLocation.getLatitude(),mLocation.getLongitude()); else Toast.makeText(this, "得不到地理位置資訊", 3).show(); } /** * 6,註冊位置跟蹤---網路追蹤/GPS追蹤 * 參數1:位置提供者 * 參數2:最小多長時間更新一次,時間都是毫秒單位 * 參數3:最小多長距離更新一次,單位米 * 參數4:註冊監聽事件LocationListener */ mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 5, this); mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this); } /** * 根據座標更新地理位置顯示 * @param lat * @param lng */private void updateMapShow(Double lat,Double lng){Double latI = lat*1E6;Double lngI = lng*1E6;mGeoPoint = new GeoPoint(latI.intValue(),lngI.intValue());mMapController.animateTo(mGeoPoint);mMapController.setCenter(mGeoPoint);//圖片的左上方為中心點//mMapController.setZoom(15);} /** * 定義內部類,可以在地圖上添加標籤,這裡在地圖上添加一張圖片標註當前位置 * @author Administrator * */ class MyOverlay extends Overlay{@Overridepublic boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when) {//取到一張圖片Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);Point out = null;//傳回值還是一個Pointout = mapView.getProjection().toPixels(mGeoPoint, out);//可以將地理座標轉換成螢幕的某個具體位置Paint paint = new Paint();//這裡不需要填充或設定if(out != null)canvas.drawBitmap(bitmap, out.x, out.y, paint);//添加一個標籤//傳回值可以是true或者不改動//return super.draw(canvas, mapView, shadow, when);return true;}/** * 點擊的時候列印出地理位置的資訊 */@Overridepublic boolean onTap(GeoPoint p, MapView mapView) {Toast.makeText(MainActivity.this, "你點擊了:"+p.getLatitudeE6()+" ; "+p.getLongitudeE6(), Toast.LENGTH_LONG).show();return super.onTap(p, mapView);} } /** * 顯示路線資訊 */@Overrideprotected boolean isRouteDisplayed() {// TODO Auto-generated method stubreturn false;}/** * 添加三個菜單 ,選擇地圖的三種顯示模式 */@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(1, 1, 1, "交通地圖");menu.add(1, 2, 3, "衛星地圖");menu.add(1, 3, 3, "街景地圖");return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch(item.getItemId()){case 1:mapView.setTraffic(true);//因為三種模式是三個不同的圖層,所以顯示的時候一次只能顯示一個圖層mapView.setSatellite(false);mapView.setStreetView(false);break;case 2:mapView.setTraffic(false);mapView.setSatellite(true);mapView.setStreetView(false);break;case 3:mapView.setTraffic(false);mapView.setSatellite(false);mapView.setStreetView(true);break;}return super.onOptionsItemSelected(item);}//-----------下面幾個方法時LocationListener介面中的方法---------/** * 位置改變時觸發 */public void onLocationChanged(Location location) {if(location != null)updateMapShow(location.getLatitude(),location.getLongitude());Toast.makeText(MainActivity.this, "你當前的位置:"+location.getLatitude()+" ; "+location.getLongitude(), 3000).show();}public void onProviderDisabled(String provider) {}/** * 位置提供者可用時觸發 */public void onProviderEnabled(String provider) {if(provider.equals(LocationManager.NETWORK_PROVIDER)){Toast.makeText(MainActivity.this, "有移動網路可用", 3000);}else if(provider.equals(LocationManager.GPS_PROVIDER)){Toast.makeText(MainActivity.this, "有GPS可用", 3000);}}public void onStatusChanged(String provider, int status,Bundle extras) {// TODO Auto-generated method stub}//移除監聽@Overrideprotected void onPause() {mLocationManager.removeUpdates(this);super.onPause();}@Overrideprotected void onResume() { mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 5, this); mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);super.onResume();}}