地图v2绘制多段线并不完全在路上 [英] map v2 drawed polylines are not exactly on the road

查看:94
本文介绍了地图v2绘制多段线并不完全在路上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个网址获取了点数,仅供测试:

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

我运行这个AsyncTask类来获取和绘制方向,这个类在Map类中用于扩展AppCompatActivity:

  private class方向扩展AsyncTask< String,Void,String> {
private Context mContext;
列表< LatLng>奔涛;
ProgressDialog对话框;
double currentLatitude,currentLongitude,destinationLatitude,destinationLongitude;
String walkDistance;

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


@Override
保护字符串doInBackground(String ... params){
JSONObject obj;


字符串响应= HttpRequest.get(https://maps.googleapis.com/maps/api/directions/json?origin=29.6628166,52.4230969&existing=27.1908698,56.1678579 &安培;键=的myKey)主体();


尝试{
System.out.println(响应内容1是+响应);

String jsonOutput = response.toString();

JSONObject jsonObject = new JSONObject(jsonOutput);

// routesArray包含所有路由
JSONArray routesArray = jsonObject.getJSONArray(routes);
//抓住第一条路线
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);


返回响应;

} catch(Exception e){
// TODO自动生成的catch块
e.printStackTrace();
}
return;


$ b @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),
新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 {
//这里是绘制地图中折线的地方
/ * 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));

* /

多边形线= googleMap.addPolygon(新PolygonOptions()
.add(新LatLng(src.latitude,src.longitude),
new LatLng(dest.latitude,dest.longitude))
.strokeColor(Color.BLUE).geodesic(true));


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

}

dialog.dismiss();


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


/ * gps =新GPSTracker(Map.this);
$ b $ //检查GPS是否启用
if(gps.canGetLocation()){

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

// \\\
是换行
Toast.makeText(getApplicationContext(),您的位置是 - \\\
Lat:+ currentLatitude +\\\
Long:+ currentLongitude ,Toast.LENGTH_LONG).show();
} else {
//无法获取位置。
// GPS或网络未启用。
//要求用户在设置中启用GPS /网络。
gps.showSettingsAlert();
} * /





}

@覆盖
保护void onProgressUpdate( Void ... values){}
}

但线路不在路上它看起来像下图:

img src =https://i.stack.imgur.com/eTM2t.jpgalt =在这里输入图片描述>

解决方法

您使用返回的 routes 对象中的 overview_polyline 来绘制路径。正如


i fetched points from this url just for test :

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

i run this AsyncTask class in order to get and draw directions, this class in used in Map class extending AppCompatActivity:

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) {}
}

but lines are not on the road and it looks like bellow image :

解决方案

You are using the overview_polyline from the returned routes object to draw your path. As the documentation says (emphasis mine)

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.

You will get a much better approximation if you draw the polyline object of each step that you receive in the legs array.

From your example (the red line represents the overview_polyline and the blue line represents the polyline object of the first step received):

这篇关于地图v2绘制多段线并不完全在路上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆