如何在兩個poi-s之間的mapview上繪製路線?在Mapview上使用兩個POI-s的Android繪圖路線
回答
通過此代碼。修改代碼,每UR要求
MapDirection.java:
public class MapDirection extends MapActivity{
MapView mapview;
MapRouteOverlay mapoverlay;
Context _context;
List<Overlay> maplistoverlay;
Drawable drawable,drawable2;
MapOverlay mapoverlay2,mapoverlay3;
GeoPoint srcpoint,destpoint;
Overlay overlayitem;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.map_direction);
RegisterActivities.registerActivity(this);
mapview=(MapView)this.findViewById(R.id.mapview);
callMap();
}
private void callMap() {
srcpoint=new GeoPoint((int)(Data.src_lat_date*1E6),(int)(Data.src_long_data*1E6));
maplistoverlay=mapview.getOverlays();
drawable=this.getResources().getDrawable(R.drawable.green_a);
mapoverlay2=new MapOverlay(drawable);
OverlayItem overlayitem = new OverlayItem(srcpoint, "", "");
mapoverlay2.addOverlay(overlayitem);
maplistoverlay.add(mapoverlay2);
destpoint=new GeoPoint((int)(Data.dest_lat_data*1E6),(int)(Data.dest_long_data*1E6));
drawable2=this.getResources().getDrawable(R.drawable.green_b);
mapoverlay3=new MapOverlay(drawable2);
OverlayItem overlayitem3 = new OverlayItem(destpoint, "", "");
mapoverlay3.addOverlay(overlayitem3);
maplistoverlay.add(mapoverlay3);
double dest_lat = Data.dest_lat_data;
double dest_long = Data.dest_long_data;
GeoPoint srcGeoPoint = new GeoPoint((int) (Data.src_lat_date* 1E6),
(int) (Data.src_long_data * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
(int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.BLUE, mapview);
mapview.getController().animateTo(srcGeoPoint);
mapview.getController().setZoom(13);
//mapview.setStreetView(true);
mapview.setBuiltInZoomControls(true);
mapview.invalidate();
}
private void DrawPath(GeoPoint src, GeoPoint dest, int color,
MapView mMapView01) {
// connect to map web service
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");//from
urlString.append(Double.toString((double)src.getLatitudeE6()/1.0E6));
urlString.append(",");
urlString.append(Double.toString((double)src.getLongitudeE6()/1.0E6));
urlString.append("&daddr=");//to
urlString.append(Double.toString((double)dest.getLatitudeE6()/1.0E6));
urlString.append(",");
urlString.append(Double.toString((double)dest.getLongitudeE6()/1.0E6));
urlString.append("&ie=UTF8&0&om=0&output=kml");
Log.d("xxx","URL="+urlString.toString());
//System.out.println(urlString);
// get the kml (XML) doc. And parse it to get the coordinates(direction route).
Document doc = null;
HttpURLConnection urlConnection= null;
URL url = null;
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());
if(doc.getElementsByTagName("GeometryCollection").getLength()>0)
{
//String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
Log.d("xxx","path="+ path);
String [] pairs = path.split(" ");
String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height
// src
GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
//mMapView01.getOverlays().add(overlayitem);
GeoPoint gp1;
GeoPoint gp2 = startGP;
for(int i=1;i<pairs.length;i++) // the last one would be crash
{
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));
mMapView01.getOverlays().add(new MapRouteOverlay(gp1,gp2,2,color));
Log.d("xxx","pair:" + pairs[i]);
}
//mMapView01.getOverlays().add(new MapRouteOverlay(dest,dest, 3)); // use the default color
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
MapRouteOverlay.java:
public class MapRouteOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
private int mode=0;
private int defaultColor;
public MapRouteOverlay(GeoPoint gp1,GeoPoint gp2,int mode) // GeoPoint is a int. (6E)
{
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
defaultColor = 999; // no defaultColor
}
public MapRouteOverlay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor)
{
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
this.defaultColor = defaultColor;
}
public int getMode()
{
return mode;
}
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);
if(mode==2)
{
if(defaultColor==999)
paint.setColor(Color.RED);
else
paint.setColor(defaultColor);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
}
}
return super.draw(canvas, mapView, shadow, when);
}
}
MapOverlay.java:
public class MapOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MapOverlay(Drawable _defaultMarker) {
super(boundCenterBottom(_defaultMarker));
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
public int size() {
return mOverlays.size();
}
}
當我使用這個,我得到: android.app.IntentReceiverLeaked:活動dk.lector.ao.mobile.shop.map.EmbeddedMapViewActivity泄露了最初註冊的IntentReceiver com.google.android[email protected]4139a3b8這裏。你是否錯過了對unregisterReceiver()的調用? 任何這可能是正確的? – 2012-05-07 07:23:19
在德國完成「der wahnsinn」:)))thx很多 – cV2 2012-05-23 13:10:40
嘿,什麼是'R.drawable.green_a'和'R.drawable.green_b'? – pawel 2012-09-13 10:50:42
- 1. 在Android MapView上繪製兩個GeoPoint之間的路線時出錯?
- 2. mapkit上的兩點繪圖路線
- 3. Android,Google地圖上的繪製路線
- 4. Android - 在MapView上繪製路徑作爲覆蓋圖
- 5. 如何在MapView中繪製路線
- 6. 在MapView上繪圖透明
- 7. 無法在android mapview中的兩個地點之間繪製路徑?
- 8. 在道路上繪製折線android
- 9. Mapview繪製路徑路徑
- 10. 如何在CloudMade上的兩個位置之間繪製路線
- 11. 在Google地圖上繪製路線
- 12. 如何在地圖上繪製路線?
- 13. 谷歌地圖上兩個位置之間的Android路線
- 14. 在Android上使用surfaceView繪製線條
- 15. MKCircle覆蓋圖未在MapView上繪製
- 16. 緩存什麼正在Android上使用MapView繪製
- 17. Android - 繪製兩個地理點之間的路線
- 18. 谷歌地圖 - MapView - 繪製一個不錯的航班路線2點
- 19. 在Android地圖上繪製路線不工作...?
- 20. 使用android上的航點繪製駕駛路線
- 21. 如何使用Google map api在Google地圖上繪製路線?
- 22. 在android地圖上繪製路徑
- 23. 使用'R'在同一圖上繪製兩條線
- 24. 如何在谷歌地圖api上繪製多個通過點的路線android
- 25. 使用自定義繪圖與MapView的奇怪繪圖artifact
- 26. 使用地圖疊加和GeoPoints在地圖視圖上的Android繪圖路徑
- 27. 在android的地圖上的兩個指定之間的路線查找?
- 28. 在mapView中爲CLLocation數組繪製路線(Swift)
- 29. 如何在Android上使用MapView顯示離線OpenStreetMap?
- 30. Android Issue Drawing使用XML解析的MapView上的長路徑
@pawel,他們是你的腳在地圖上我想,可能是你可以使用png圖像文件。 – vtokmak 2012-09-25 22:31:25