2015-09-05 44 views
1

您好,我正在開發一個應用程序,顯示具有當前位置和經度和緯度值的地圖並更改該位置。每5分鐘將這些經度和緯度值存儲在sqlitedatabase中,並且正在使這些數據庫值正常工作。但是,如何使用存儲的數據庫值繪製路徑。請有人告訴我解決方案。如何在android中的地圖中從數據庫中繪製路徑緯度和經度值

代碼:

PolylineOptions polylineOptions = new PolylineOptions(); 
    polylineOptions.color(Color.RED); 
    polylineOptions.width(5); 
    double latitude=Double.parseDouble(c.getString(0)); 
    double longitude=Double.parseDouble(c.getString(1)); 

    LatLng latlngval=new LatLng(latitude, longitude); 
    Log.e("path", ""+latlngval); 
    ArrayList<LatLng> latlnglist=new ArrayList<LatLng>(); 
    latlnglist.add(latlngval); 

    for (int i = 0; i < latlnglist.size(); i++) { 

     polylineOptions.add(latlnglist.get(i)); 

    } 
+0

你使用的是什麼地圖從你的數據庫中的值?谷歌的,這裏還是OSM? – Wilik

+0

正在使用谷歌地圖 – user3612165

回答

1

您可以使用Polyline

PolylineOptions polylineOptions = new PolylineOptions(); 
polylineOptions.color(Color.RED); 
polylineOptions.width(5); 
Cursor c = database.query(...); //select the latitude and longitude column 
while (c.moveToNext()) { 
    polylineOptions.add(new LatLng(Double.parseDouble(c.getString(0)), Double.parseDouble(c.getString(1)))); 
} 
googleMap.addPolyline(polylineOptions); 
+0

感謝您的迴應。我正在發送一個代碼告訴我,這是正確的或不。 – user3612165

+0

上午編輯代碼看一次是否正確。 – user3612165

+0

請給我確切的代碼片段.. – user3612165

0
public class DrawrootTask extends AsyncTask<String, String, String> { 
private Context context; 
public static boolean flagCompleted = false; 
private GoogleMap googleMap; 
private double source_lat = 0.0; 
private double source_long = 0.0; 
private double dest_lat = 0.0; 
private double dest_long = 0.0; 
Userdata userdata; 
String tag = "DrawRootTask"; 
private ProgressDialog progressDialog; 
public static double dist, time; 
private Polyline line; 
String distanceText = ""; 
String durationText = ""; 

public DrawrootTask(Context context, LatLng source, LatLng destination, 
     GoogleMap googleMap) { 
    source_lat = source.latitude; 
    source_long = source.longitude; 
    dest_lat = destination.latitude; 
    dest_long = destination.longitude; 

    this.googleMap = googleMap; 
    this.context = context; 
    userdata = Userdata.getinstance(context); 

} 

protected void onPreExecute() { 
    // // TODO Auto-generated method stub 
    super.onPreExecute(); 
    progressDialog = new ProgressDialog(context); 
    progressDialog.setMessage(context.getResources().getString(
      R.string.please_wait)); 
    progressDialog.setIndeterminate(true); 
    progressDialog.show(); 

} 

@Override 
protected String doInBackground(String... params) { 

    String json = ""; 

    // constructor 
    StringBuilder urlString = new StringBuilder(); 
    urlString.append("http://maps.googleapis.com/maps/api/directions/json"); 
    HashMap<String, String> keyValue = new HashMap<String, String>(); 
    urlString.append("?origin=");// from 
    urlString.append(Double.toString(source_lat)); 
    urlString.append(","); 
    urlString.append(Double.toString(source_long)); 
    urlString.append("&destination=");// to 
    urlString.append(Double.toString(dest_lat)); 
    urlString.append(","); 
    urlString.append(Double.toString(dest_long)); 
    urlString.append("&sensor=false&mode=driving&alternatives=true"); 

    // defaultHttpClient 
    String url = urlString.toString(); 
    FetchUrl fetchurl = new FetchUrl(); 
    json = fetchurl.fetchUrl(url, keyValue); 

    Log.e("Buffer Error", json); 
    return json; 

} 

@Override 
protected void onPostExecute(String result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 
    try { 
     progressDialog.dismiss(); 
     final JSONObject json = new JSONObject(result); 
     JSONArray routeArray = json.getJSONArray("routes"); 
     JSONObject routes = routeArray.getJSONObject(0); 
     JSONObject overviewPolylines = routes 
       .getJSONObject("overview_polyline"); 
     String encodedString = overviewPolylines.getString("points"); 
     List<LatLng> list = decodePoly(encodedString); 

     for (int z = 0; z < list.size() - 1; z++) { 
      LatLng src = list.get(z); 
      LatLng dest = list.get(z + 1); 
      line = googleMap.addPolyline(new PolylineOptions() 
        .add(new LatLng(src.latitude, src.longitude), 
          new LatLng(dest.latitude, dest.longitude)) 
        // .width(8).color(Color.BLUE).geodesic(true)); 
        .width(8) 
        .color(context.getResources().getColor(
          R.color.actionbar_color)).geodesic(true)); 
      Log.i("draw root", "" + "" + line.toString()); 
     } 
     JSONArray legs = routes.getJSONArray("legs"); 
     JSONObject steps = legs.getJSONObject(0); 
     JSONObject duration = steps.getJSONObject("duration"); 
     JSONObject distance = steps.getJSONObject("distance"); 
     distanceText = distance.getString("text"); 
     durationText = duration.getString("text"); 
     Log.i("draw root", "" + distance.toString()); 
     dist = Double.parseDouble(distance.getString("text").replaceAll(
       "[^\\.]", "")); 
     time = Double.parseDouble(duration.getString("text").replaceAll(
       "[^\\.]", "")); 
     userdata.setDistance(dist); 
     userdata.setTime(time); 
     Log.d(tag, "distace is " + dist + " time is " + time); 
     flagCompleted = true; 
    } catch (JSONException e) { 
     Log.d("draw root", "" + e); 

    } 
} 

private List<LatLng> decodePoly(String encoded) { 

    List<LatLng> poly = new ArrayList<LatLng>(); 
    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; 

     LatLng p = new LatLng((((double) lat/1E5)), 
       (((double) lng/1E5))); 
     poly.add(p); 
    } 

    return poly; 
} 

}

+0

感謝您的回覆,但這是我認爲的路線方向代碼.. – user3612165

相關問題