如何在android中从地图数据库中绘制路径纬度和经度值
您好,我正在开发一个应用程序,我正在用当前位置和经纬度值显示地图,并更改该位置。每隔5分钟,我将这些纬度和经度值存储在sqlitedatabase中,并获取这些数据库值,使其正常工作。但是,如何使用存储的数据库值绘制路径。请告诉我解决办法 代码:如何在android中从地图数据库中绘制路径纬度和经度值,android,sqlite,google-maps,Android,Sqlite,Google Maps,您好,我正在开发一个应用程序,我正在用当前位置和经纬度值显示地图,并更改该位置。每隔5分钟,我将这些纬度和经度值存储在sqlitedatabase中,并获取这些数据库值,使其正常工作。但是,如何使用存储的数据库值绘制路径。请告诉我解决办法 代码: PolylineOptions PolylineOptions=新的PolylineOptions(); polylineOptions.color(color.RED); 多段线选项。宽度(5); 双纬度=double.parseDouble(c.g
PolylineOptions PolylineOptions=新的PolylineOptions();
polylineOptions.color(color.RED);
多段线选项。宽度(5);
双纬度=double.parseDouble(c.getString(0));
double longitude=double.parseDouble(c.getString(1));
LatLng latlngval=新LatLng(纬度、经度);
Log.e(“路径”、“+latlngval”);
ArrayList latlnglist=新的ArrayList();
latlnglist.add(latlngval);
对于(int i=0;i
您可以使用
公共类DrawrootTask扩展了AsyncTask{
私人语境;
公共静态布尔flagCompleted=false;
私人谷歌地图谷歌地图;
专用双源_lat=0.0;
专用双源_long=0.0;
私人双目的地=0.0;
私人双dest_long=0.0;
用户数据用户数据;
String tag=“DrawRootTask”;
私有进程对话;
公共静态双区,时间;
专用多段线;
字符串距离text=“”;
字符串durationText=“”;
公共DrawrootTask(上下文、LatLng源、LatLng目标、,
谷歌地图(谷歌地图){
震源纬度=震源纬度;
source_long=source.longitude;
目的地纬度=目的地纬度;
dest_long=目的地经度;
this.googleMap=谷歌地图;
this.context=上下文;
userdata=userdata.getinstance(上下文);
}
受保护的void onPreExecute(){
////TODO自动生成的方法存根
super.onPreExecute();
progressDialog=新建progressDialog(上下文);
progressDialog.setMessage(context.getResources().getString(
请稍等;
progressDialog.setUndeterminate(true);
progressDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串json=“”;
//建造师
StringBuilder urlString=新的StringBuilder();
urlString.append(“http://maps.googleapis.com/maps/api/directions/json");
HashMap keyValue=新HashMap();
urlString.append(“?origin=”);//来自
append(Double.toString(source_lat));
urlString.append(“,”);
append(Double.toString(source_long));
urlString.append(“&destination=”);//到
append(Double.toString(dest_lat));
urlString.append(“,”);
append(Double.toString(dest_long));
追加(“&sensor=false&mode=driving&alternations=true”);
//defaultHttpClient
字符串url=urlString.toString();
FetchUrl FetchUrl=新的FetchUrl();
json=fetchurl.fetchurl(url,keyValue);
Log.e(“缓冲区错误”,json);
返回json;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
试一试{
progressDialog.disclose();
最终JSONObject json=新JSONObject(结果);
JSONArray routarray=json.getJSONArray(“路由”);
JSONObject routes=routeArray.getJSONObject(0);
JSONObject概览多段线=管线
.getJSONObject(“概述”和“多段线”);
字符串编码字符串=概览多段线。获取字符串(“点”);
列表=解码多边形(编码字符串);
对于(intz=0;z>1):(结果>>1));
lat+=dlat;
移位=0;
结果=0;
做{
b=编码的.charAt(索引++)-63;
结果|=(b&0x1f)=0x20);
int-dlng=((结果&1)!=0?~(结果>>1):(结果>>1));
液化天然气+=液化天然气;
LatLng p=新LatLng(((双)lat/1E5)),
((双)液化天然气/1E5));
poly.add(p);
}
返回多边形;
}
}也许这会对你有所帮助 检索值,而不是采用硬编码值(如示例中所示)
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));
}
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);
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(
"[^\\.0123456789]", ""));
time = Double.parseDouble(duration.getString("text").replaceAll(
"[^\\.0123456789]", ""));
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;
}