android在Map上划线

最近在搞在地图上画出路线图,经过一段时间的摸索,终于搞明白了,其实也挺简单的,写个类继承Overlay,并重写draw方法,在draw方法中画出 path即可。对于Overaly,在地图上标记某个点或者画线之类的就要使用overlay,overlay相当于一个覆盖物,覆盖在地图上,这个覆盖物要自己实现所以要继承Overlay。

[文件] MapActivity.java ~ 3KB    下载(410)

01 package net.blogjava.mobile.map;  
02  
03 import java.util.List;  
04 import Android.app.AlertDialog;  
05 import android.graphics.Bitmap;  
06 import android.graphics.BitmapFactory;  
07 import android.graphics.Canvas;  
08 import android.graphics.Color;  
09 import android.graphics.Paint;  
10 import android.graphics.Path;  
11 import android.graphics.Point;  
12 import android.location.Address;  
13 import android.location.Geocoder;  
14 import android.os.Bundle;  
15 import android.view.Menu;  
16 import com.google.android.maps.GeoPoint;  
17 import com.google.android.maps.MapActivity;  
18 import com.google.android.maps.MapController;  
19 import com.google.android.maps.MapView;  
20 import com.google.android.maps.Overlay;  
21 import com.google.android.maps.Projection;  
22  
23 public class Main extends MapActivity {  
24 private GeoPoint gpoint1, gpoint2, gpoint3;// 连线的点  
25 @Override 
26 public void onCreate(Bundle savedInstanceState) {  
27     super.onCreate(savedInstanceState);  
28     setContentView(R.layout.main);  
29     MapView mapView = (MapView) findViewById(R.id.mapview);  
30     mapView.setClickable(true);  
31     mapView.setBuiltInZoomControls(true);  
32     MapController mapController = mapView.getController();  
33     mapView.setTraffic(true);// 交通图  
34     // mapView.setSatellite(true);//卫星图  
35     // mapView.setStreetView(true);//街景  
36     MyOverlay myOverlay = new MyOverlay();  
37     mapView.getOverlays().add(myOverlay);  
38     mapController.setZoom(15);// 初始放大倍数  
39     gpoint1 = new GeoPoint((int) (24.477384 1000000),  
40         (int) (118.158216 1000000));  
41     gpoint2 = new GeoPoint((int) (24.488967 1000000),  
42         (int) (118.144277 1000000));  
43     gpoint3 = new GeoPoint((int) (24.491091 1000000),  
44         (int) (118.136781 1000000));  
45     mapController.animateTo(gpoint1);  
46 }  
47 @Override 
48 protected boolean isRouteDisplayed() {  
49     // TODO Auto-generated method stub  
50     return false;  
51 }  
52 class MyOverlay extends Overlay {  
53     @Override 
54     public void draw(Canvas canvas, MapView mapView, boolean shadow) {  
55     // TODO Auto-generated method stub  
56     super.draw(canvas, mapView, shadow);  
57     // 画笔  
58     Paint paint = new Paint();  
59     paint.setColor(Color.RED);  
60     paint.setDither(true);  
61     paint.setStyle(Paint.Style.STROKE);  
62     paint.setStrokeJoin(Paint.Join.ROUND);  
63     paint.setStrokeCap(Paint.Cap.ROUND);  
64     paint.setStrokeWidth(2);  
65     Projection projection = mapView.getProjection();  
66     Point p1 = new Point();  
67     Point p2 = new Point();  
68     Point p3 = new Point();  
69     projection.toPixels(gpoint1, p1);  
70     projection.toPixels(gpoint2, p2);  
71     projection.toPixels(gpoint3, p3);  
72     Path path = new Path();  
73     path.moveTo(p1.x, p1.y);  
74     path.lineTo(p2.x, p2.y);  
75     path.lineTo(p3.x, p3.y);  
76     canvas.drawPath(path, paint);// 画出路径  
77     }  
78 }  
79

[代码] 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" android:layout_width="fill_parent" 
4     android:layout_height="fill_parent"
5     <com.google.android.maps.MapView 
6         android:id="@+id/mapview" android:layout_width="fill_parent" 
7         android:layout_height="fill_parent"   
8         android:apiKey="0IB7Kn70qp1LT216Hhb-jmHJ8GLTie4p63O77KQ" /> 
9 </LinearLayout>

[代码] [其他]代码

1 最后别忘了加权限 :
2  
3 <uses-permission Android:name="android.permission.INTERNET"/>
4  
5 在<applacation></applacation>之间加<uses-library Android:name="com.google.android.maps" />

[代码] 绘制路线图

01    /** 
02     * 通过解析google map返回的xml,在map中画路线图 
03     */ 
04    public void drawRoute(){  
05            
06        String url = "http://maps.google.com/maps/api/directions/xml?origin=23.055291,113.391802" +  
07                "&destination=23.046604,113.397510&sensor=false&mode=walking";  
08            
09        HttpGet get = new HttpGet(url);  
10        String strResult = "";  
11        try {  
12            HttpParams httpParameters = new BasicHttpParams();  
13            HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);  
14            HttpClient httpClient = new DefaultHttpClient(httpParameters);   
15                
16            HttpResponse httpResponse = null;  
17            httpResponse = httpClient.execute(get);  
18                
19            if (httpResponse.getStatusLine().getStatusCode() == 200){  
20                strResult = EntityUtils.toString(httpResponse.getEntity());  
21            }  
22        catch (Exception e) {  
23            return;  
24        }  
25            
26        if (-1 == strResult.indexOf("<status>OK</status>")){  
27            Toast.makeText(this"获取导航路线失败!", Toast.LENGTH_SHORT).show();  
28            this.finish();  
29            return;  
30        }  
31            
32        int pos = strResult.indexOf("<overview_polyline>");  
33        pos = strResult.indexOf("<points>", pos + 1);  
34        int pos2 = strResult.indexOf("</points>", pos);  
35        strResult = strResult.substring(pos + 8, pos2);  
36            
37        List<GeoPoint> points = decodePoly(strResult);  
38            
39        MyOverLay mOverlay = new MyOverLay(points);  
40        List<Overlay> overlays = mMapView.getOverlays();  
41        overlays.add(mOverlay);  
42            
43        if (points.size() >= 2){  
44            mMapController.animateTo(points.get(0));  
45        }  
46             
47        mMapView.invalidate();  
48    }  
49   
50   
51 /** 
52     * 解析返回xml中overview_polyline的路线编码 
53     *  
54     * @param encoded 
55     * @return 
56     */ 
57    private List<GeoPoint> decodePoly(String encoded) {  
58   
59        List<GeoPoint> poly = new ArrayList<GeoPoint>();  
60        int index = 0, len = encoded.length();  
61        int lat = 0, lng = 0;  
62   
63        while (index < len) {  
64            int b, shift = 0, result = 0;  
65            do {  
66                b = encoded.charAt(index++) - 63;  
67                result |= (b & 0x1f) << shift;  
68                shift += 5;  
69            while (b >= 0x20);  
70            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));  
71            lat += dlat;  
72   
73            shift = 0;  
74            result = 0;  
75            do {  
76                b = encoded.charAt(index++) - 63;  
77                result |= (b & 0x1f) << shift;  
78                shift += 5;  
79            while (b >= 0x20);  
80            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));  
81            lng += dlng;  
82   
83            GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),  
84                 (int) (((double) lng / 1E5) * 1E6));  
85            poly.add(p);  
86        }  
87   
88        return poly;  
89   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值