效果图如下
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/white" xmlns:android="http://schemas.android.com/apk/res/android" > <com.google.android.maps.MapView android:id="@+id/myMapView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_x="0px" android:layout_y="82px" android:enabled="true" android:clickable="true" android:apiKey="0-6xYD0vU1u6OFOd5pue1lrB_kqXXrIQib3vQhw" /> </LinearLayout> package com.test.activity; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; 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.Overlay; import com.google.android.maps.OverlayItem; import com.test.activity.util.HelloItemizedOverlay; import com.test.activity.util.MyOverLay; public class MainActivity extends MapActivity { private MapView mMapView; private MapController mMapController; private LocationManager mLocationManager; private Location mLocation; private String mLocationPrivider = ""; private int zoomLevel = 0; private GeoPoint gp1; private GeoPoint gp2; private boolean _run = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* 建立MapView对象 */ mMapView = (MapView) findViewById(R.id.myMapView1); mMapController = mMapView.getController(); mMapView.setBuiltInZoomControls(true); /* 设定预设的放大层级 */ zoomLevel = 17; mMapController.setZoom(zoomLevel); /* Provider初始化 */ mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); /* 取得Provider与Location */ getLocationPrivider(); if (mLocation != null) { /* 取得目前的经纬度 */ gp1 = getGeoByLocation(mLocation); gp2 = gp1; /* 将MapView的中点移至目前位置 */ refreshMapView(); /* 设定事件的Listener */ mLocationManager.requestLocationUpdates(mLocationPrivider, 2000, 10, mLocationListener); gp1 = gp2; /* 清除Overlay */ resetOverlay(); /* 画起点 */ setStartPoint(); /* 更新MapView */ refreshMapView(); /* 重设移动距离为0,并更新TextView */ /* 启动画路线的机制 */ _run = true; } else { new AlertDialog.Builder(MainActivity.this).setTitle("系统讯息") .setMessage(getResources().getString(R.string.app_name)) .setNegativeButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MainActivity.this.finish(); } }).show(); } } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); /* 画终点 */ setEndPoint(); /* 更新MapView */ refreshMapView(); /* 终止画路线的机制 */ _run = false; } /* MapView的Listener */ public final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { /* 如果记录进行中,就画路线并更新移动距离 */ if (_run) { /* 记下移动后的位置 */ gp2 = getGeoByLocation(location); /* 画路线 */ setRoute(); /* 更新MapView */ refreshMapView(); /* 取得移动距离 */ List<Overlay> mapOverlays = mMapView.getOverlays(); Drawable drawable = MainActivity.this.getResources().getDrawable(R.drawable.a); HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,MainActivity.this); OverlayItem overlayitem2 = new OverlayItem(gp2, "Hello!", "仓井空今晚和我动作交流"); itemizedoverlay.addOverlay(overlayitem2); mapOverlays.add(itemizedoverlay); gp1 = gp2; } } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }; /* 取得GeoPoint的method */ private GeoPoint getGeoByLocation(Location location) { GeoPoint gp = null; try { if (location != null) { double geoLatitude = location.getLatitude() * 1E6; double geoLongitude = location.getLongitude() * 1E6; gp = new GeoPoint((int) geoLatitude, (int) geoLongitude); } } catch (Exception e) { e.printStackTrace(); } return gp; } /* 取得LocationProvider */ public void getLocationPrivider() { Criteria mCriteria = new Criteria(); mCriteria.setAccuracy(Criteria.ACCURACY_COARSE); mCriteria.setAltitudeRequired(false); mCriteria.setBearingRequired(false); mCriteria.setCostAllowed(true); mCriteria.setPowerRequirement(Criteria.POWER_LOW); mLocationPrivider = mLocationManager.getBestProvider(mCriteria, true); mLocation = mLocationManager.getLastKnownLocation(mLocationPrivider); } /* 设定起点的method */ private void setStartPoint() { int mode = 1; MyOverLay mOverlay = new MyOverLay(gp1, gp2, mode); List<Overlay> overlays = mMapView.getOverlays(); overlays.add(mOverlay); } /* 设定路线的method */ private void setRoute() { int mode = 2; MyOverLay mOverlay = new MyOverLay(gp1, gp2, mode); List<Overlay> overlays = mMapView.getOverlays(); overlays.add(mOverlay); } /* 设定终点的method */ private void setEndPoint() { int mode = 3; MyOverLay mOverlay = new MyOverLay(gp1, gp2, mode); List<Overlay> overlays = mMapView.getOverlays(); overlays.add(mOverlay); } /* 重设Overlay的method */ private void resetOverlay() { List<Overlay> overlays = mMapView.getOverlays(); overlays.clear(); } /* 更新MapView的method */ public void refreshMapView() { mMapView.displayZoomControls(true); MapController myMC = mMapView.getController(); myMC.animateTo(gp2); myMC.setZoom(zoomLevel); mMapView.setSatellite(false); } /* format移动距离的method */ public String format(double num) { NumberFormat formatter = new DecimalFormat("###"); String s = formatter.format(num); return s; } @Override protected boolean isRouteDisplayed() { return false; } }
package com.test.activity.util; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.graphics.RectF; 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 MyOverLay extends Overlay { private GeoPoint gp1; private GeoPoint gp2; private int mRadius=6; private int mode=0; /* 建构子,传入起点与终点的GeoPoint与mode */ public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode) { this.gp1 = gp1; this.gp2 = gp2; this.mode = mode; } @Override public boolean draw (Canvas canvas, MapView mapView, boolean shadow, long when) { Projection projection = mapView.getProjection(); if (shadow == false) { /* 设定笔刷 */ Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.BLACK); Point point = new Point(); projection.toPixels(gp1, point); /* mode=1:建立起点 */ if(mode==1) { /* 定义RectF对象 */ RectF oval=new RectF(point.x - mRadius, point.y - mRadius, point.x + mRadius, point.y + mRadius); /* 绘制起点的圆形 */ canvas.drawOval(oval, paint); } /* mode=2:画路线 */ else if(mode==2) { Point point2 = new Point(); projection.toPixels(gp2, point2); paint.setStrokeWidth(5); paint.setAlpha(120); /* 画线 */ canvas.drawLine(point.x, point.y, point2.x,point2.y, paint); } /* mode=3:建立终点 */ else if(mode==3) { /* 避免误差,先画最后一段的路线 */ Point point2 = new Point(); projection.toPixels(gp2, point2); paint.setStrokeWidth(5); paint.setAlpha(120); canvas.drawLine(point.x, point.y, point2.x,point2.y, paint); /* 定义RectF对象 */ RectF oval=new RectF(point2.x - mRadius,point2.y - mRadius, point2.x + mRadius,point2.y + mRadius); /* 绘制终点的圆形 */ paint.setAlpha(255); canvas.drawOval(oval, paint); } } return super.draw(canvas, mapView, shadow, when); } }
package com.test.activity.util; import java.util.ArrayList; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.graphics.drawable.Drawable; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.OverlayItem; public class HelloItemizedOverlay extends ItemizedOverlay { private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); private Context mContext; public HelloItemizedOverlay(Drawable defaultMarker) { //super(defaultMarker); super(boundCenterBottom(defaultMarker)); // TODO Auto-generated constructor stub } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } @Override protected OverlayItem createItem(int i) { // TODO Auto-generated method stub return mOverlays.get(i); } @Override public int size() { // TODO Auto-generated method stub return mOverlays.size(); } public HelloItemizedOverlay(Drawable defaultMarker, Context context) { //super(defaultMarker); this(defaultMarker); //mContext = context; this.mContext = context; } @Override protected boolean onTap(int index) { OverlayItem item = mOverlays.get(index); Dialog dialog = new Dialog(mContext); dialog.setTitle(item.getSnippet()); dialog.show(); return true; } }
总共三个类。。。
速度 上班了 。。。。今天就到这吧