时间:2021-05-02
本文实例讲述了Android实现在map上画出路线的方法。分享给大家供大家参考。具体如下:
最近在搞在地图上画出路线图,经过一段时间的摸索,终于搞明白了,其实也挺简单的,写个类继承Overlay,并重写draw方法,在draw方法中画出 path即可。对于Overaly,在地图上标记某个点或者画线之类的就要使用overlay,overlay相当于一个覆盖物,覆盖在地图上,这个覆盖物要自己实现所以要继承Overlay。
MapActivity.java如下:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 package net.blogjava.mobile.map; import java.util.List; import Android.app.AlertDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Point; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.view.Menu; 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.Projection; public class Main extends MapActivity { private GeoPoint gpoint1, gpoint2, gpoint3;// 连线的点 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView) findViewById(R.id.mapview); mapView.setClickable(true); mapView.setBuiltInZoomControls(true); MapController mapController = mapView.getController(); mapView.setTraffic(true);// 交通图 // mapView.setSatellite(true);//卫星图 // mapView.setStreetView(true);//街景 MyOverlay myOverlay = new MyOverlay(); mapView.getOverlays().add(myOverlay); mapController.setZoom(15);// 初始放大倍数 gpoint1 = new GeoPoint((int) (24.477384 * 1000000), (int) (118.158216 * 1000000)); gpoint2 = new GeoPoint((int) (24.488967 * 1000000), (int) (118.144277 * 1000000)); gpoint3 = new GeoPoint((int) (24.491091 * 1000000), (int) (118.136781 * 1000000)); mapController.animateTo(gpoint1); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } class MyOverlay extends Overlay { @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { // TODO Auto-generated method stub super.draw(canvas, mapView, shadow); // 画笔 Paint paint = new Paint(); paint.setColor(Color.RED); paint.setDither(true); paint.setStyle(Paint.Style.STROKE); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth(2); Projection projection = mapView.getProjection(); Point p1 = new Point(); Point p2 = new Point(); Point p3 = new Point(); projection.toPixels(gpoint1, p1); projection.toPixels(gpoint2, p2); projection.toPixels(gpoint3, p3); Path path = new Path(); path.moveTo(p1.x, p1.y); path.lineTo(p2.x, p2.y); path.lineTo(p3.x, p3.y); canvas.drawPath(path, paint);// 画出路径 } } }main.xml如下:
? 1 2 3 4 5 6 7 8 9 <?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:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0IB7Kn70qp1LT216Hhb-jmHJ8GLTie4p63O77KQ" /> </LinearLayout> 最后别忘了加权限 :
<uses-permission Android:name="android.permission.INTERNET"/>
在<applacation></applacation>之间加<uses-library Android:name="com.google.android.maps" />
绘制路线图:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 /** * 通过解析google map返回的xml,在map中画路线图 */ public void drawRoute(){ String url = "http://maps.google.com/maps/api/directions/xml?origin=23.055291,113.391802" + "&destination=23.046604,113.397510&sensor=false&mode=walking"; HttpGet get = new HttpGet(url); String strResult = ""; try { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); HttpClient httpClient = new DefaultHttpClient(httpParameters); HttpResponse httpResponse = null; httpResponse = httpClient.execute(get); if (httpResponse.getStatusLine().getStatusCode() == 200){ strResult = EntityUtils.toString(httpResponse.getEntity()); } } catch (Exception e) { return; } if (-1 == strResult.indexOf("<status>OK</status>")){ Toast.makeText(this, "获取导航路线失败!", Toast.LENGTH_SHORT).show(); this.finish(); return; } int pos = strResult.indexOf("<overview_polyline>"); pos = strResult.indexOf("<points>", pos + 1); int pos2 = strResult.indexOf("</points>", pos); strResult = strResult.substring(pos + 8, pos2); List<GeoPoint> points = decodePoly(strResult); MyOverLay mOverlay = new MyOverLay(points); List<Overlay> overlays = mMapView.getOverlays(); overlays.add(mOverlay); if (points.size() >= 2){ mMapController.animateTo(points.get(0)); } mMapView.invalidate(); } /** * 解析返回xml中overview_polyline的路线编码 * * @param encoded * @return */ private List<GeoPoint> decodePoly(String encoded) { List<GeoPoint> poly = new ArrayList<GeoPoint>(); int index = 0, len = encoded.length(); int lat = 0, lng = 0; while (index < len) { int b, shift = 0, result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += dlng; GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6),(int) (((double) lng / 1E5) * 1E6)); poly.add(p); } return poly; }希望本文所述对大家的Android程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了微信小程序使用map组件实现路线规划功能。分享给大家供大家参考,具体如下:效果图实现原理1.通过map组件标记起始点和绘制路线图;2.通过高德地图
本文实例介绍了Android如何画出触摸轨迹的方法,分享给大家供大家参考,具体内容如下效果图:实现代码:packagecom.android.gameview5
本文实例讲述了Android开发实现绘制淘宝收益图折线效果。分享给大家供大家参考,具体如下:实现的效果我一会贴上,我先说下原理,我们知道要实现在canvas上画
imagej测量长度的步骤是: 1、选择直线工具; 2、在标尺上画出单位长度的直线; 3、选择Analyze、SetScale,Knowndistance
用word可以画出如图中的楼梯效果。如何做到?详细操作步骤请往下看。步骤1、新建文档;在文档的绘图工具栏中点“矩形”图标;在文档上画出图