私はアプリをしていますGoogle Map android。 私はマップ内の2つのポイントの間に短いパスを描きたい。 しかし、私は知らないどのように私は必要ですか? 助けてください。本当にありがとうございます。地図の2点間に短い道を描きます
0
A
答えて
0
新しいクラスutil.java
public class Util {
private DataBaseHelper dbHelper;
private GeoFencApplicationDataset Dataset;
private int getId;
public static boolean checkConnection(Context mContext) {
NetworkInfo info = ((ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
return true;
}
return true;
}
public void DrawPath(Context c, GeoPoint src, GeoPoint dest, int color,
MapView mMapView01) {
Dataset = (GeoFencApplicationDataset) c.getApplicationContext();
dbHelper = Dataset.getDbHelper();
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());
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) {
final String path = doc.getElementsByTagName("GeometryCollection")
.item(0).getFirstChild().getFirstChild()
.getFirstChild().getNodeValue();
final String[] pairs = path.split(" ");
String[] lngLat = pairs[0].split(",");
final GeoPoint startGP = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays().add(
new PathOverLay(startGP, startGP, 1));
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) {
lngLat = pairs[i].split(",");
gp1 = gp2;
gp2 = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
mMapView01.getOverlays().add(
new PathOverLay(gp1, gp2, 2, color));
if (getId != 0) {
dbHelper.insertTempLatLong(lngLat[1], lngLat[0], getId);
}
Log.d("xxx", "pair:" + pairs[i]);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
public double[] getLocation(Context c) {
Criteria criteria;
String provider;
LocationManager locManager = (LocationManager) c
.getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
provider = locManager.getBestProvider(criteria, true);
double[] loca = new double[2];
// For Latitude & Longitude
Location location = locManager.getLastKnownLocation(provider);
if (location != null) {
loca[0] = location.getLatitude();
loca[1] = location.getLongitude();
} else {
loca[0] = 0.0;
loca[1] = 0.0;
}
return loca;
}
public void setId(int id) {
// TODO Auto-generated method stub
getId = id;
}
}
新しいクラスPathOverLay.java
public class PathOverLay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
private int mRadius = 6;
private int mode = 0;
private int defaultColor;
private String text = "";
private Bitmap img = null;
public PathOverLay(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 PathOverLay(GeoPoint gp1, GeoPoint gp2, int mode, int defaultColor) {
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
this.defaultColor = defaultColor;
}
public void setText(String t) {
this.text = t;
}
public void setBitmap(Bitmap bitmap) {
this.img = bitmap;
}
public int getMode() {
return mode;
}
@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);
// mode=1:start
if (mode == 1) {
if (defaultColor == 999)
paint.setColor(Color.BLUE);
else
paint.setColor(defaultColor);
// RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
// point.x + mRadius, point.y + mRadius);
RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
point.x + mRadius, point.y + mRadius);
// start point
canvas.drawOval(oval, paint);
}
// mode=2:path
else 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(70);
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
}
/* mode=3:end */
else if (mode == 3) {
/* the last path */
if (defaultColor == 999)
paint.setColor(Color.GREEN);
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);
RectF oval = new RectF(point2.x - mRadius, point2.y - mRadius,
point2.x + mRadius, point2.y + mRadius);
/* end point */
paint.setAlpha(120);
canvas.drawOval(oval, paint);
}
}
return super.draw(canvas, mapView, shadow, when);
}
// Read more:
// http://csie-tw.blogspot.com/2009/06/android-driving-direction-route-path.html#ixzz1hte9kGoi
}
最終呼び出し方法... [上のライン/パスを描画するの
util = new Util();
mapOverlays = mapView.getOverlays();
util = new Util();
util.setId(id);
GeoPoint srcGeoPoint = new GeoPoint((int) (destLat * 1E6),
(int) (destLong * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (srcLat * 1E6),
(int) (srcLong * 1E6));
progrDialog = ProgressDialog.show(AddFenceActivity.this,
"", "Please Wait..", true);
mapView.getOverlays().add(
new PathOverLay(srcGeoPoint,destGeoPoint, 2,Color.RED));
util.DrawPath(AddFenceActivity.this, srcGeoPoint,
destGeoPoint, Color.RED, mapView);
mapView.getController().animateTo(srcGeoPoint);
public class MapOverLayItem extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public MapOverLayItem(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public MapOverLayItem(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
}
+0
ありがとうございます。私はそれを行うことができます.thanhs –
+0
@Androidは最短パスはオフラインですか? –
関連する問題
- 1. MVC 2 - URL短縮/ルーティング(地図ルート)
- 2. 道順のカスタム地図
- 3. 地図の中間地点に到達したときの判断方法
- 4. 剣道線図を使用して、データマーカー(点)を無地にする方法は?
- 5. ビン地図を描く?
- 6. 特定の地域の道路地図データを抽出する
- 7. Google静的地図上の2つの都市間のパスを描く
- 8. 2点間の測地線距離
- 9. 地図上に経路を描く(アンドロイド)
- 10. Google静的地図にパスを描画
- 11. ビン地図apiを使って道路を描くにはどうすればいいですか?
- 12. 2点間のパスを描く
- 13. 緯度と経度を指定して地図に点を描画する
- 14. 短い道 - ラジオボタン
- 15. 短い道
- 16. 地図上に線(道順)を描くことに問題がありますか?
- 17. フレックスの線図に点を描く
- 18. d3とtopojsonで地図を描く
- 19. 2つの地理的地点間の距離を計算する方法
- 20. キャンバス上の2点間にアークを描画する方法は?
- 21. 小さい地図を大きな地図に統合する方法
- 22. 地図内の地図java
- 23. 地図2(地図)と他のRxJS関数を公開していない角2のHttpサービス
- 24. 地図もコレクションにできますか?
- 25. 2点間の経路を描き、kmlにエクスポートする方法は?
- 26. 2つの異なる点の間に線を描く
- 27. 地図上に多くのマーカーを描画する方法
- 28. アンドロイドの地図にオーバーレイを描画する方法は?
- 29. 現在の地理座標を割り当てて現在の地点から目標地点までの道を引き出す方法は?
- 30. エンドユーザがビン地図(バーチャルアース)に図形を描けるようにする
可能重複Googleマップ](http://stackoverflow.com/questions/2176397/drawing-a-line-path-on-google-maps) – Fraser
と - http://stackoverflow.co m/questions/2941792/how-to-draw-a-line-points-in-google-map-in-android – Fraser