每一台电脑都要申请属于自己的android:apiKey,要是使用别人的android:apiKey,
则地图只显示方格,不会有实际的地图出现,并且在Android虚拟机重建或者重装电脑的操作系统的时候
也要重新申请android:apiKey,关于如何申请,我在“申请Google Map服务”中已说得很详细。
新建一个地图项目。
在main.xml中:
<?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">
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:clickable="true" android:enabled="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0Pm9QrsSh_mwtc6rMyqZMRu71qFpIB51UXVWHmg" />
</LinearLayout>
在MyOverlayImpl.java中:
package com.li.googlemapproject;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class MyOverlayImpl extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> allOverlayItems = new ArrayList<OverlayItem>();
private Context context = null;
public MyOverlayImpl(Drawable defaultMarker, Context context) {
super(boundCenter(defaultMarker));
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return this.allOverlayItems.get(i);
}
@Override
public int size() {
return this.allOverlayItems.size();
}
@Override
protected boolean onTap(int index) { // 单击标记图片之后的操作
OverlayItem item = this.allOverlayItems.get(index); // 取得指定的点
Dialog dialog = new AlertDialog.Builder(this.context)
.setIcon(R.drawable.pic_m).setTitle(item.getTitle())
.setMessage(item.getSnippet())
.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
return true;
}
public void addOverlayItem(OverlayItem item) {
this.allOverlayItems.add(item);
super.populate();
}
}
在PaintLineOverlay.java中:
package com.li.googlemapproject;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class PaintLineOverlay extends Overlay {
private GeoPoint beginGeoPoint = null;
private GeoPoint endGeoPoint = null;
public PaintLineOverlay(GeoPoint beginGeoPoint, GeoPoint endGeoPoint) {
this.beginGeoPoint = beginGeoPoint;
this.endGeoPoint = endGeoPoint;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Paint paint = new Paint();
paint.setStyle(Style.FILL_AND_STROKE);
paint.setStrokeWidth(3);
paint.setColor(Color.RED);
Point beginPoint = new Point();
Point endPoint = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(this.beginGeoPoint, beginPoint);
projection.toPixels(this.endGeoPoint, endPoint);
canvas.drawLine(beginPoint.x, beginPoint.y, endPoint.x, endPoint.y,
paint);
}
}
在PaintPointOverlay.java中:
package com.li.googlemapproject;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class PaintPointOverlay extends Overlay {
private GeoPoint geoPoint = null;
public PaintPointOverlay(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Point point = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(this.geoPoint, point); // 将地图上坐标的点设置为绘图屏幕的点
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawCircle(point.x, point.y, 6, paint);
}
}
在MyGoogleMapDemo.java中:
package com.li.googlemapproject;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
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;
public class MyGoogleMapDemo extends MapActivity {
private MapView mapView = null;
private int longitudeE6 = 0;
private int latitudeE6 = 0;
private LocationManager locationManager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mapView = (MapView) super.findViewById(R.id.mapview); // 取得地图视图
this.mapView.setBuiltInZoomControls(true);
// 给定一个坐标:北海银滩的坐标:109.15,21.40
this.longitudeE6 = (int) (109.15 * 1E6);
this.latitudeE6 = (int) (21.40 * 1E6);
GeoPoint point = new GeoPoint(this.latitudeE6, this.longitudeE6); // 要标记的坐标
MyLocationOverlay myloc = new MyLocationOverlay(this, this.mapView);
myloc.enableMyLocation(); // 注册GPS更新我的位置
myloc.enableCompass(); // 开启磁场感应
this.mapView.getOverlays().add(myloc);
MapController mapController = this.mapView.getController();
mapController.animateTo(point); // 设置坐标的动画
mapController.setCenter(point);
mapController.setZoom(16); // 最大的级别是16
this.locationManager = (LocationManager) super
.getSystemService(Context.LOCATION_SERVICE);
this.locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, new LocationListenerImpl());
}
private class LocationListenerImpl implements LocationListener {
public void onLocationChanged(Location location) {
MyGoogleMapDemo.this.longitudeE6 = (int) (location.getLongitude() * 1E6);
MyGoogleMapDemo.this.latitudeE6 = (int) (location.getLatitude() * 1E6);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
在AndroidManifest.xml中修改权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.googlemapproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyGoogleMapDemo"
android:label="@string/title_activity_my_google_map_demo" >
<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" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>