介绍了如何通过解析KML文件,画出地图上两点之间的路线
谷歌地图提供了几种版本的,可以运用于网页,应用程序,手机应用等等,使用Google Direction API,谷歌地图可以方便地查找出方便地查询出地图上从A点到B点的路线图。
Google Direction API提供了更丰富的路线信息数据。由于访问量过大吧,谷歌现在不提供了查询路线的接口API。对于G粉来说,不能不说是遗憾了,本文提供了在谷歌地图上画路线图的另一种方法,也就是通过URL访问谷歌地图,解析KML文件,获得路线图数据,重新画在您的Android谷歌地图应用上。
第一步,新建Android项目,填写项目名称,勾选编译的包,这次选择了Google APIs 8,填入应用名称,包名称,以及Activity。
第二步,设计应用界面,修改main.xml
第三步,填写运行时,首次启动Activity,SmartActivity继承MapActivity
public class SmartActivity extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
第四步,新建名为DirectionPathOverly继承Overlay,也就是地图上两点之间画直线图层
public class DirectionPathOverlay extends Overlay { private GeoPoint gp1; private GeoPoint gp2; public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) { this.gp1 = gp1; this.gp2 = gp2; } @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); Point point = new Point(); projection.toPixels(gp1, point); paint.setColor(Color.BLUE); Point point2 = new Point(); projection.toPixels(gp2, point2); paint.setStrokeWidth(2); canvas.drawLine((float) point.x, (float) point.y, (float) point2.x, (float) point2.y, paint); } return super.draw(canvas, mapView, shadow, when); } @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { super.draw(canvas, mapView, shadow); } }
第五步,在启动Activity中添加函数getDirectionData(),访问网络,获取两个位置之间的KML文件数据,解析KML文件,获取路线的数据列表。
private String[] getDirectionData(String srcPlace, String destPlace) { String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="+srcPlace+"&daddr="+destPlace+"&ie=UTF&om=0&output=kml"; Log.d("URL", urlString); Document doc = null; HttpURLConnection urlConnection = null; URL url = null; String pathConent = ""; try{ url = new URL(urlString.toString()); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.connect(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(urlConnection.getInputStream()); } catch (Exception e){} NodeList nl = doc.getElementsByTagName("LineString"); for(int s=0; s< nl.getLength(); s++){ Node rootNode = nl.item(s); NodeList configItems = rootNode.getChildNodes(); for(int x=0; x < configItems.getLength(); x++) { Node lineStringNode = configItems.item(x); NodeList path = lineStringNode.getChildNodes(); pathConent = path.item(0).getNodeValue(); } } String[] tempContent = pathConent.split(" "); return tempContent; }
第六步,实验画出地图上两点之间的路线折线,在启动Activity的OnCreate()函数中添加如下代码:
myMapView = (MapView) findViewById(R.id.mapview); geoPoint = null; myMapView.setSatellite(false); String pairs[] = getDirectionData("ahmedabad", "vadodara"); String[] lngLat = pairs[0].split(","); // STARTING POINT GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1]) * 1E6), (int) (Double.parseDouble(lngLat[0]) * 1E6)); myMC = myMapView.getController(); geoPoint = startGP; myMC.setCenter(geoPoint); myMC.setZoom(8); myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP)); // NAVIGATE THE PATH GeoPoint gp1; GeoPoint gp2 = startGP; for (int i = 1; i < pairs.length; i++) { lngLat = pairs[i].split(","); gp1 = gp2; // watch out! For GeoPoint, first:latitude, second:longitude gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),(int) (Double.parseDouble(lngLat[0]) * 1E6)); myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2)); Log.d("xxx", "pair:" + pairs[i]); } // END POINT myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2)); myMapView.getController().animateTo(startGP); myMapView.setBuiltInZoomControls(true); myMapView.displayZoomControls(true);
好了,代码完成了,运行Run As “Android Project”,结果如下图所示
![]()