android–map v2 drawed折线并不完全在路上

android–map v2 drawed折线并不完全在路上,第1张

概述我从这个网址中获取了一些点用于测试:https://maps.googleapis.com/maps/api/directions/json?origin=29.6628166,52.4230969&destination=27.1908698,56.1678579&key=mykey我运行这个AsyncTask类以获取和绘制方向,这个类在Map类中用于扩展AppCompatActivity:privateclassdi

我从这个网址中获取了一些点用于测试:

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

但是线路不在路上,它看起来像波纹管图像:

解决方法:

您正在使用返回的路径对象中的overvIEw_polyline来绘制路径.正如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.

如果绘制在leg数组中接收的每个步骤的折线对象,则会得到更好的近似值.

从您的示例中(红线代表overvIEw_polyline,蓝线代表收到的第一步的折线对象):

总结

以上是内存溢出为你收集整理的android – map v2 drawed折线并不完全在路上全部内容,希望文章能够帮你解决android – map v2 drawed折线并不完全在路上所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099309.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存