Android实现在map上画出路线的方法
程序员文章站
2024-01-04 10:53:40
本文实例讲述了android实现在map上画出路线的方法。分享给大家供大家参考。具体如下:
最近在搞在地图上画出路线图,经过一段时间的摸索,终于搞明白了,其实也挺简单的,...
本文实例讲述了android实现在map上画出路线的方法。分享给大家供大家参考。具体如下:
最近在搞在地图上画出路线图,经过一段时间的摸索,终于搞明白了,其实也挺简单的,写个类继承overlay,并重写draw方法,在draw方法中画出 path即可。对于overaly,在地图上标记某个点或者画线之类的就要使用overlay,overlay相当于一个覆盖物,覆盖在地图上,这个覆盖物要自己实现所以要继承overlay。
mapactivity.java如下:
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如下:
<?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" />
绘制路线图:
/** * 通过解析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程序设计有所帮助。