ANDROID_MARS学习笔记_S03_009_GOOGLEMAP3

、代码

1.xml
(1)main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <com.google.android.maps.MapView
 8         android:id="@+id/mapViewId"
 9         android:layout_width="fill_parent"
10         android:layout_height="fill_parent"
11         android:enabled="true"
12         android:clickable="true"
13         android:apiKey="0pkT0EYxPi2VZ5beDaJ0g08aCtWGmKTFnOvj6iw"
14         />
15 </LinearLayout>

 

(2)AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3       package="com.se7en"
 4       android:versionCode="1"
 5       android:versionName="1.0">
 6     <uses-sdk android:minSdkVersion="8" />
 7 
 8     <application android:icon="@drawable/icon" android:label="@string/app_name">
 9         <uses-library android:name="com.google.android.maps" />
10         <activity android:name=".MainActivity"
11                   android:label="@string/app_name">
12             <intent-filter>
13                 <action android:name="android.intent.action.MAIN" />
14                 <category android:name="android.intent.category.LAUNCHER" />
15             </intent-filter>
16         </activity>
17     </application>
18     <uses-permission android:name="android.permission.INTERNET" />
19 </manifest>

 

2.java
(1)MainActivity.java

  1 package com.se7en;
  2 
  3 import java.util.List;
  4 
  5 import android.graphics.Bitmap;
  6 import android.graphics.BitmapFactory;
  7 import android.graphics.Canvas;
  8 import android.graphics.Color;
  9 import android.graphics.Paint;
 10 import android.graphics.Path;
 11 import android.graphics.Point;
 12 import android.os.Bundle;
 13 
 14 import com.google.android.maps.GeoPoint;
 15 import com.google.android.maps.MapActivity;
 16 import com.google.android.maps.MapController;
 17 import com.google.android.maps.MapView;
 18 import com.google.android.maps.Overlay;
 19 import com.google.android.maps.Projection;
 20 
 21 public class MainActivity extends MapActivity {
 22     private Projection projection;
 23     private List<Overlay> overlays;
 24     private MapController mapController;
 25     private GeoPoint beginGeoPoint;
 26     private GeoPoint endGeoPoint;
 27     
 28     /** Called when the activity is first created. */
 29     @Override
 30     public void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.main);
 33         
 34         beginGeoPoint = new GeoPoint(19240000,-99120000);
 35         endGeoPoint = new GeoPoint(19340000,-99220000);
 36         
 37         MapView mapView = (MapView)findViewById(R.id.mapViewId);
 38         mapView.setBuiltInZoomControls(true);
 39         
 40         //mapController主要用于对地图进行控制
 41         mapController = mapView.getController();
 42         overlays = mapView.getOverlays();
 43         projection = mapView.getProjection();
 44         
 45         overlays.add(new PointOverlay(beginGeoPoint));
 46         overlays.add(new PointOverlay(endGeoPoint));
 47         overlays.add(new LineOverlay(beginGeoPoint,endGeoPoint));
 48         
 49         //将地图以动画的形式移动到指定的点
 50         mapController.animateTo(beginGeoPoint);
 51         //设置地图的放大级别
 52         mapController.setZoom(12);
 53     }
 54 
 55     @Override
 56     protected boolean isRouteDisplayed() {
 57         // TODO Auto-generated method stub
 58         return false;
 59     }
 60     
 61     //图标层
 62     class PointOverlay extends Overlay{
 63         private GeoPoint geoPoint;
 64         public PointOverlay(){
 65             
 66         }
 67         public PointOverlay(GeoPoint geoPoint){
 68             this.geoPoint = geoPoint;
 69         }
 70         public void draw(Canvas canvas,MapView mapView,boolean shadow){
 71             super.draw(canvas, mapView, shadow);
 72             Point point = new Point();
 73             //将geoPoint转换为屏幕上的X、Y轴坐标
 74             projection.toPixels(geoPoint, point);
 75             //位图
 76             Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.tool);
 77             Paint paint = new Paint();//画笔
 78             //Y轴-30是为了调整图片的显示位置(默认是讲图片的左上角对应到经纬度点)
 79             canvas.drawBitmap(bmp,point.x,point.y-30,paint);
 80         }
 81     }
 82     
 83     //路线层
 84     class LineOverlay extends Overlay{
 85         private GeoPoint begin;
 86         private GeoPoint end;
 87         
 88         public LineOverlay(){}
 89         public LineOverlay(GeoPoint begin,GeoPoint end){
 90             this.begin = begin;
 91             this.end = end;
 92         }
 93         
 94         public void draw(Canvas canvas,MapView mapView,boolean shadow){
 95             super.draw(canvas, mapView, shadow);
 96             Paint paint = new Paint();
 97             paint.setColor(Color.BLUE);
 98             //设置线条的样式(填满的空心线)
 99             paint.setStyle(Paint.Style.FILL_AND_STROKE);
100             paint.setStrokeWidth(2);
101             Point beginPoint = new Point();
102             Point endPoint = new Point();
103             Path path = new Path();
104             projection.toPixels(begin,beginPoint);
105             projection.toPixels(end,endPoint);
106             //指定划线的起始点
107             path.moveTo(beginPoint.x,beginPoint.y);
108             //指定划线的终点
109             path.lineTo(endPoint.x,endPoint.y);
110             canvas.drawPath(path,paint);
111         }
112     }
113     
114 }

 

 

 

你可能感兴趣的:(ANDROID_MARS学习笔记_S03_009_GOOGLEMAP3)