• 在google地图上画出两个地点的行走路线


    Google Direction API提供了更丰富的路线信息数据。由于访问量过大吧,谷歌现在不提供了查询路线的接口API,现在要想获得一个路线我们可以通过URL访问google然后解析得到的KML文件,得到路线的每个点的经纬度,然互在一个图层上画出这些点。代码来自于对一些网上资源的整理,一共提供了连个方法,一个通过两个地方的名字,一个是通过两个地点的经纬度,但是有时候会出现错误的别是对中国的城市,还有就是别用中文,要不会很惨,希望有人给出一个更好的方法

    [图片] 未命名1.jpg

    [代码] 新建一个图层

    01 package com.android.antkingwei.google.direction;
    02
    03 import android.graphics.Canvas;
    04 import android.graphics.Color;
    05 import android.graphics.Paint;
    06 import android.graphics.Point;
    07
    08 import com.google.android.maps.GeoPoint;
    09 import com.google.android.maps.MapView;
    10 import com.google.android.maps.Overlay;
    11 import com.google.android.maps.Projection;
    12
    13 public class DirectionPathOverlay extends Overlay {
    14 private GeoPoint gp1;
    15 private GeoPoint gp2;
    16
    17 public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) {
    18 this.gp1 = gp1;
    19 this.gp2 = gp2;
    20 }
    21
    22 @Override
    23 public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
    24 Projection projection = mapView.getProjection();
    25 if(shadow == false) {
    26 Paint paint = new Paint();
    27 paint.setAntiAlias(true);
    28 Point point = new Point();
    29 projection.toPixels(gp1, point);
    30 paint.setColor(Color.BLUE);
    31 Point point2 = new Point();
    32 projection.toPixels(gp2, point2);
    33 paint.setStrokeWidth(2);
    34 canvas.drawLine((float) point.x, (float) point.y, (float) point2.x, (float) point2.y, paint);
    35 }
    36 return super.draw(canvas, mapView, shadow, when);
    37 }
    38
    39 @Override
    40 public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    41 super.draw(canvas, mapView, shadow);
    42 }
    43 }

    [代码] 主类

    001 package com.android.antkingwei.google.direction;
    002
    003 import java.net.HttpURLConnection;
    004 import java.net.URL;
    005
    006 import javax.xml.parsers.DocumentBuilder;
    007 import javax.xml.parsers.DocumentBuilderFactory;
    008
    009 import org.w3c.dom.Document;
    010 import org.w3c.dom.Node;
    011 import org.w3c.dom.NodeList;
    012
    013 import com.google.android.maps.GeoPoint;
    014 import com.google.android.maps.MapActivity;
    015 import com.google.android.maps.MapController;
    016 import com.google.android.maps.MapView;
    017
    018 import android.app.Activity;
    019 import android.os.Bundle;
    020 import android.util.Log;
    021
    022 public class MainActivity extends MapActivity {
    023 private MapView myMapView;
    024 private GeoPoint geoPoint;
    025 private MapController myMC;
    026 private String[] pairs;
    027 /** Called when the activity is first created. */
    028 @Override
    029 public void onCreate(Bundle savedInstanceState) {
    030 super.onCreate(savedInstanceState);
    031 setContentView(R.layout.main);
    032
    033 myMapView = (MapView) findViewById(R.id.mapview);
    034 geoPoint = null;
    035 myMapView.setSatellite(false);
    036 //通过地名
    037 //pairs = getDirectionData("ahmedabad", "vadodara");
    038 //通过四个坐标
    039 pairs = getUrl(22.5348,113.97246,35.422006,119.524095);
    040 String[] lngLat = pairs[0].split(",");
    041 // STARTING POINT
    042 GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1]) * 1E6), (int) (Double.parseDouble(lngLat[0]) * 1E6));
    043 myMC = myMapView.getController();
    044 geoPoint = startGP;
    045 myMC.setCenter(geoPoint);
    046 myMC.setZoom(8);
    047 myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));
    048 // NAVIGATE THE PATH
    049 GeoPoint gp1;
    050 GeoPoint gp2 = startGP;
    051 for (int i = 1; i < pairs.length; i++) {
    052 lngLat = pairs[i].split(",");
    053 gp1 = gp2;
    054 // watch out! For GeoPoint, first:latitude, second:longitude
    055 gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),(int) (Double.parseDouble(lngLat[0]) * 1E6));
    056 myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
    057 Log.d("xxx", "pair:" + pairs[i]);
    058 }
    059 // END POINT
    060 myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));
    061 myMapView.getController().animateTo(startGP);
    062 myMapView.setBuiltInZoomControls(true);
    063 myMapView.displayZoomControls(true);
    064
    065 }
    066 @Override
    067 protected boolean isRouteDisplayed() {
    068 // TODO Auto-generated method stub
    069 return false;
    070 }
    071 /**
    072 * 通过四个坐标点,获得KML然后通过DOM解析
    073 * @param fromLat
    074 * @param fromLon
    075 * @param toLat
    076 * @param toLon
    077 * @return
    078 */
    079 public String[] getUrl(double fromLat,double fromLon,double toLat,double toLon){
    080 String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="+fromLat+","+fromLon+"&daddr="+toLat+","+toLon+"&ie=UTF8&0&om=0&output=kml";
    081 Log.d("URL", urlString);
    082 Document doc = null;
    083 HttpURLConnection urlConnection = null;
    084 URL url = null;
    085 String pathConent = "";
    086
    087 try{
    088 url = new URL(urlString.toString());
    089 urlConnection = (HttpURLConnection) url.openConnection();
    090 urlConnection.setRequestMethod("GET");
    091 urlConnection.setDoOutput(true);
    092 urlConnection.setDoInput(true);
    093 urlConnection.connect();
    094 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    095 DocumentBuilder db = dbf.newDocumentBuilder();
    096 doc = db.parse(urlConnection.getInputStream());
    097 } catch (Exception e){}
    098 NodeList nl = doc.getElementsByTagName("LineString");
    099 for(int s=0; s< nl.getLength(); s++){
    100 Node rootNode = nl.item(s);
    101 NodeList configItems = rootNode.getChildNodes();
    102 for(int x=0; x < configItems.getLength(); x++) {
    103 Node lineStringNode = configItems.item(x);
    104 NodeList path = lineStringNode.getChildNodes();
    105 pathConent = path.item(0).getNodeValue();
    106 }
    107 }
    108 String[] tempContent = pathConent.split(" ");
    109 return tempContent;
    110 }
    111 /**
    112 * 通过连个地名获得KML然后进行DOM解析
    113 * @param srcPlace
    114 * @param destPlace
    115 * @return
    116 */
    117 private String[] getDirectionData(String srcPlace, String destPlace) {
    118 String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="+srcPlace+"&daddr="+destPlace+"&ie=UTF&om=0&output=kml";
    119 Log.d("URL", urlString);
    120 Document doc = null;
    121 HttpURLConnection urlConnection = null;
    122 URL url = null;
    123 String pathConent = "";
    124 try{
    125 url = new URL(urlString.toString());
    126 urlConnection = (HttpURLConnection) url.openConnection();
    127 urlConnection.setRequestMethod("GET");
    128 urlConnection.setDoOutput(true);
    129 urlConnection.setDoInput(true);
    130 urlConnection.connect();
    131 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    132 DocumentBuilder db = dbf.newDocumentBuilder();
    133 doc = db.parse(urlConnection.getInputStream());
    134 } catch (Exception e){}
    135 NodeList nl = doc.getElementsByTagName("LineString");
    136 for(int s=0; s< nl.getLength(); s++){
    137 Node rootNode = nl.item(s);
    138 NodeList configItems = rootNode.getChildNodes();
    139 for(int x=0; x < configItems.getLength(); x++) {
    140 Node lineStringNode = configItems.item(x);
    141 NodeList path = lineStringNode.getChildNodes();
    142 pathConent = path.item(0).getNodeValue();
    143 }
    144 }
    145 String[] tempContent = pathConent.split(" ");
    146 return tempContent;
    147 }
    148 }

    [代码] main.xml

    01 <?xml version="1.0" encoding="utf-8"?>
    02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    03 android:orientation="vertical"
    04 android:layout_width="fill_parent"
    05 android:layout_height="fill_parent"
    06 >
    07 <TextView
    08 android:layout_width="fill_parent"
    09 android:layout_height="wrap_content"
    10 android:text="@string/hello"
    11 />
    12 <com.google.android.maps.MapView
    13 xmlns:android="http://schemas.android.com/apk/res/android"
    14 android:id="@+id/mapview"
    15 android:layout_width="fill_parent"
    16 android:layout_height="fill_parent"
    17 android:enabled="true"
    18 android:clickable="true"
    19 android:apiKey="0FZLYf-YM4SRrJrJum55MeeaO4Gd_IitVFmtUeA"/>
    20 </LinearLayout>

    [代码] AndroidManifest.xml

    01 <?xml version="1.0" encoding="utf-8"?>
    02 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    03 package="com.android.antkingwei.google.direction"
    04 android:versionCode="1"
    05 android:versionName="1.0">
    06 <uses-sdk android:minSdkVersion="7" />
    07
    08 <application android:icon="@drawable/icon" android:label="@string/app_name">
    09 <activity android:name=".MainActivity"
    10 android:label="@string/app_name">
    11 <intent-filter>
    12 <action android:name="android.intent.action.MAIN" />
    13 <category android:name="android.intent.category.LAUNCHER" />
    14 </intent-filter>
    15
    16 </activity>
    17 <uses-library android:name="com.google.android.maps" />
    18 </application>
    19 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    20 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    21 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    22 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    23 </manifest>
  • 相关阅读:
    代码发布概述图
    gitPython模块
    django重点面试题
    paramiko模块
    Spring Security 注解
    JsonIgnoreProperties JsonIgnore导致RequestBody无法接受参数值
    Http和Rpc区别
    ExceptionHandler(思路参考CustomException)
    Linux安装Nginx
    秒杀扣除库存方案
  • 原文地址:https://www.cnblogs.com/tuncaysanli/p/2469020.html
Copyright © 2020-2023  润新知