gpt4 book ai didi

android - map v2 绘制的折线不完全在路上

转载 作者:行者123 更新时间:2023-11-29 19:31:12 24 4
gpt4 key购买 nike

我从这个 url 获取点只是为了测试:

https://maps.googleapis.com/maps/api/directions/json?origin=29.6628166, 52.4230969&destination=27.1908698,56.1678579&key=mykey

我运行这个 AsyncTask 类是为了获取和绘制方向,这个类在扩展 AppCompatActivity 的 Map 类中使用:

private class direction extends AsyncTask<String, Void, String> {
private Context mContext;
List<LatLng> pontos;
ProgressDialog dialog;
double currentLatitude, currentLongitude,destinationLatitude,destinationLongitude;
String walkDistance;

public direction (Context context,LatLng point){
mContext = context;
destinationLatitude = point.latitude;
destinationLongitude = point.longitude;
}


@Override
protected String doInBackground(String... params) {
JSONObject obj;


String response = HttpRequest.get("https://maps.googleapis.com/maps/api/directions/json?origin=29.6628166, 52.4230969&destination=27.1908698,56.1678579&key=mykey").body();


try {
System.out.println("Response content 1 was " + response);

String jsonOutput = response.toString();

JSONObject jsonObject = new JSONObject(jsonOutput);

// routesArray contains ALL routes
JSONArray routesArray = jsonObject.getJSONArray("routes");
// Grab the first route
JSONObject route = routesArray.getJSONObject(0);

JSONArray legs = route.getJSONArray("legs");
JSONObject firtsLegs = legs.getJSONObject(0);

JSONObject distance = firtsLegs.getJSONObject("distance");


System.out.println("Response test was : " + distance.getString("text"));

walkDistance = distance.getString("text");

//Toast.makeText(getApplicationContext(), "فاصله شما تا نقطه: " + distance.getString("text"), Toast.LENGTH_LONG).show();


JSONObject poly = route.getJSONObject("overview_polyline");
String polyline = poly.getString("points");
pontos = decodePoly(polyline);


return response;

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";

}

@Override
protected void onPostExecute(String result) {

Toast.makeText(getApplicationContext(), "distance : "+walkDistance, Toast.LENGTH_LONG).show();

/*System.out.println("Response content 2 was " + result);
Polygon polygon = mapFragment.addPolygon(new PolygonOptions()
.add(new LatLng(35.7513974, 51.4350038),
new LatLng(35.7713974, 51.4350038),
new LatLng(35.7913974, 51.4350038),
new LatLng(35.7913974, 51.5350038))
.strokeWidth(5)
.strokeColor(Color.BLUE));*/


for (int i = 0; i < pontos.size() - 1; i++) {
LatLng src = pontos.get(i);
LatLng dest = pontos.get(i + 1);
try{
//here is where it will draw the polyline in your map
/*Polyline line = googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(10).color(Color.BLUE).geodesic(false));

*/

Polygon line = googleMap.addPolygon(new PolygonOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.strokeColor(Color.BLUE).geodesic(true));



}catch(NullPointerException e){
Log.e("Error", "NullPointerException onPostExecute: " + e.toString());
}catch (Exception e2) {
Log.e("Error", "Exception onPostExecute: " + e2.toString());
}

}

dialog.dismiss();
}

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(Map.this);
dialog.setMessage("در حال مشخص کردن مسیر شما هستیم، لطفا صبر کنید.");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.show();


/*gps = new GPSTracker(Map.this);

// Check if GPS enabled
if(gps.canGetLocation()) {

currentLatitude = gps.getLatitude();
currentLongitude = gps.getLongitude();

// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + currentLatitude + "\nLong: " + currentLongitude, Toast.LENGTH_LONG).show();
} else {
// Can't get location.
// GPS or network is not enabled.
// Ask user to enable GPS/network in settings.
gps.showSettingsAlert();
}*/





}

@Override
protected void onProgressUpdate(Void... values) {}
}

但是道路上没有线,看起来像下面的图片:

enter image description here

最佳答案

您正在使用 overview_polyline来自返回的 routes绘制路径的对象。作为the documentation说(强调我的)

overview_polyline contains a single points object that holds an encoded polyline representation of the route. This polyline is an approximate (smoothed) path of the resulting directions.

如果绘制 polyline,您将获得更好的近似值每个 step 的对象您在 legs 中收到的数组。

根据您的示例(红线代表 overview_polyline,蓝线代表第一个 polyline 收到的 step 对象):

enter image description here

关于android - map v2 绘制的折线不完全在路上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39910838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com