欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)在map上畫出路線的方法

 更新時間:2015年07月21日 11:23:10   作者:華宰  
這篇文章主要介紹了Android實現(xiàn)在map上畫出路線的方法,較為詳細(xì)的分析了Android在map上繪制路線所涉及的map圖調(diào)用、畫筆的使用、頁面布局及權(quán)限控制的相關(guān)技巧,需要的朋友可以參考下

本文實例講述了Android實現(xiàn)在map上畫出路線的方法。分享給大家供大家參考。具體如下:

最近在搞在地圖上畫出路線圖,經(jīng)過一段時間的摸索,終于搞明白了,其實也挺簡單的,寫個類繼承Overlay,并重寫draw方法,在draw方法中畫出 path即可。對于Overaly,在地圖上標(biāo)記某個點或者畫線之類的就要使用overlay,overlay相當(dāng)于一個覆蓋物,覆蓋在地圖上,這個覆蓋物要自己實現(xiàn)所以要繼承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);//衛(wèi)星圖 
 // mapView.setStreetView(true);//街景 
 MyOverlay myOverlay = new MyOverlay(); 
 mapView.getOverlays().add(myOverlay); 
 mapController.setZoom(15);// 初始放大倍數(shù) 
 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>

最后別忘了加權(quán)限 :
 
<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, "獲取導(dǎo)航路線失敗!", 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程序設(shè)計有所幫助。

相關(guān)文章

  • Android實現(xiàn)層疊卡片式banner

    Android實現(xiàn)層疊卡片式banner

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)層疊卡片式banner,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • 九宮圖比較常用的多控件布局(GridView)使用介紹

    九宮圖比較常用的多控件布局(GridView)使用介紹

    GridView跟ListView都是比較常用的多控件布局,而GridView更是實現(xiàn)九宮圖的首選,下面與大家分享下GridView用法,感興趣的朋友可以參考下哈
    2013-06-06
  • Android 再按一次返回鍵退出程序?qū)崿F(xiàn)思路

    Android 再按一次返回鍵退出程序?qū)崿F(xiàn)思路

    用戶退出應(yīng)用前給出一個提示是很有必要的,因為可能是用戶并不真的想退出,而只是一不小心按下了返回鍵,大部分應(yīng)用的做法是在應(yīng)用退出去前給出一個Dialog提示框;個人覺得再按一次返回鍵退出程序很有必要,接下來介紹一些簡單實現(xiàn)
    2013-01-01
  • Android網(wǎng)絡(luò)訪問之Retrofit使用教程

    Android網(wǎng)絡(luò)訪問之Retrofit使用教程

    Retrofit?是一個?RESTful?的?HTTP?網(wǎng)絡(luò)請求框架的封裝,網(wǎng)絡(luò)請求的工作本質(zhì)上是?OkHttp?完成,而?Retrofit?僅負(fù)責(zé)?網(wǎng)絡(luò)請求接口的封裝
    2022-12-12
  • Android實現(xiàn)3D云標(biāo)簽效果

    Android實現(xiàn)3D云標(biāo)簽效果

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)3D云標(biāo)簽效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android 多用戶詳情

    Android 多用戶詳情

    這篇文章主要以Linux 用戶和組詳細(xì)的介紹了Android 多用戶的內(nèi)容,感興趣的小伙伴可以參考下文,希望文章能get到你的點
    2021-08-08
  • Android中多個ContentProvider的初始化順序詳解

    Android中多個ContentProvider的初始化順序詳解

    在日常Android開發(fā)中經(jīng)常會寫一些sdk來供他人或者自己調(diào)用,一般這些sdk都涉及到初始化,下面這篇文章主要給大家介紹了關(guān)于Android中多個ContentProvider的初始化順序的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Android自定義實現(xiàn)日歷控件

    Android自定義實現(xiàn)日歷控件

    這篇文章主要為大家詳細(xì)介紹了Android自定義實現(xiàn)日歷控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • android中圖片的三級緩存cache策略(內(nèi)存/文件/網(wǎng)絡(luò))

    android中圖片的三級緩存cache策略(內(nèi)存/文件/網(wǎng)絡(luò))

    實現(xiàn)圖片緩存也不難,需要有相應(yīng)的cache策略。這里我采用 內(nèi)存-文件-網(wǎng)絡(luò) 三層cache機制,其中內(nèi)存緩存包括強引用緩存和軟引用緩存(SoftReference),其實網(wǎng)絡(luò)不算cache,這里姑且也把它劃到緩存的層次結(jié)構(gòu)中
    2013-06-06
  • Flutter中漸變色的使用案例分享

    Flutter中漸變色的使用案例分享

    在日常的開發(fā)中,UI為了讓界面更加吸引人往往會在界面上用到大量的漸變色,本文將通過幾個案例更好的去了解Flutter中漸變色的使用,需要的可以參考一下
    2023-06-06

最新評論