久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

標記之間的 Google Maps API 和自定義折線路線

Google Maps API and custom polyline route between Markers(標記之間的 Google Maps API 和自定義折線路線)
本文介紹了標記之間的 Google Maps API 和自定義折線路線的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想為一個安卓應用創建一個自定義路由,我不確定我應該使用哪個 API 以及它是否與 Java 兼容.

據我所知,我需要使用航路點來制定路線(我不需要知道兩點之間的距離,只是為了制定路線).

目標是從地圖一側的菜單中選擇一個選項,并顯示兩個標記之間的自定義路線之一.

解決方案

您可以使用

I'd like to make a custom route for an android app, I'm not sure which API should I use and if it is compatible with Java.

As far as I know I need to use waypoints to make a route (I don't need to know the distance between the two points, just to make a route).

The objective is to choose an option from a menu on the side of the map and show one of the custom routes between two Markers.

解決方案

You can do this using the Google Maps API v2 for Android, and the Google Maps Directions webservice API

For getting started with the Google Maps API, there are plenty of other good answers already. See here for a complete working example of a simple map Activity. Note that you'll also need to get an API key set up to work with your project.

As for using the Google Maps Directions webservice API, you should first read the documentation. You can use an API key and enable the API in your developer console, but it still works currently without using an API key.

Here is the basic code you'll need in order to use the Google Maps API to draw a Polyline between two points, note that the points returned from the API are encoded in a base 64 encoded String that needs to be decoded.

First, ensure that your project includes the Google Maps Utility library, which will be used to decode the base64 encoded polyline:

dependencies {
    compile 'com.google.maps.android:android-maps-utils:0.5+'
    //.......
}

Here is the AsyncTask, that you should give two LatLng points to when calling it.

You would call the AsyncTask with two LatLng objects, for example between two Markers:

new GetDirectionsAsync().execute(markerOne.getPosition(), markerTwo.getPosition());

Here is the AsyncTask code:

class GetDirectionsAsync extends AsyncTask<LatLng, Void, List<LatLng>> {

    JSONParser jsonParser;
    String DIRECTIONS_URL = "https://maps.googleapis.com/maps/api/directions/json";


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected List<LatLng> doInBackground(LatLng... params) {
        LatLng start = params[0];
        LatLng end = params[1];

        HashMap<String, String> points = new HashMap<>();
        points.put("origin", start.latitude + "," + start.longitude);
        points.put("destination", end.latitude + "," + end.longitude);

        jsonParser = new JSONParser();

        JSONObject obj = jsonParser.makeHttpRequest(DIRECTIONS_URL, "GET", points, true);

        if (obj == null) return null;

        try {
            List<LatLng> list = null;

            JSONArray routeArray = obj.getJSONArray("routes");
            JSONObject routes = routeArray.getJSONObject(0);
            JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
            String encodedString = overviewPolylines.getString("points");
            list = PolyUtil.decode(encodedString);

            return list;

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(List<LatLng> pointsList) {

        if (pointsList == null) return;

        if (line != null){
            line.remove();
        }

        PolylineOptions options = new PolylineOptions().width(5).color(Color.MAGENTA).geodesic(true);
        for (int i = 0; i < pointsList.size(); i++) {
            LatLng point = pointsList.get(i);
            options.add(point);
        }
        line = mMap.addPolyline(options);

    }
}

The AsyncTask references some member variables of the Activity, namely the Polyline and the GoogleMap, the Activity definition would look like this:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback{

    GoogleMap mMap;
    Polyline line;
    //.....

Here's the JSONParser class used in this example, note that this is a modified version updated for android-23 that I wrote a blog post about:

public class JSONParser {

    String charset = "UTF-8";
    HttpURLConnection conn;
    DataOutputStream wr;
    StringBuilder result;
    URL urlObj;
    JSONObject jObj = null;
    StringBuilder sbParams;
    String paramsString;

    public JSONObject makeHttpRequest(String url, String method,
                                      HashMap<String, String> params, boolean encode) {

        sbParams = new StringBuilder();
        int i = 0;
        for (String key : params.keySet()) {
            try {
                if (i != 0){
                    sbParams.append("&");
                }
                if (encode) {
                    sbParams.append(key).append("=")
                            .append(URLEncoder.encode(params.get(key), charset));
                }
                else{
                    sbParams.append(key).append("=")
                            .append(params.get(key));
                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            i++;
        }

        if (method.equals("POST")) {
            // request method is POST
            try {
                urlObj = new URL(url);

                conn = (HttpURLConnection) urlObj.openConnection();

                conn.setDoOutput(true);

                conn.setRequestMethod("POST");

                conn.setRequestProperty("Accept-Charset", charset);

                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);

                conn.connect();

                paramsString = sbParams.toString();

                wr = new DataOutputStream(conn.getOutputStream());
                wr.writeBytes(paramsString);
                wr.flush();
                wr.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else if(method.equals("GET")){
            // request method is GET

            if (sbParams.length() != 0) {
                url += "?" + sbParams.toString();
            }

            Log.d("JSONParser", "full GET url: " + url);

            try {
                urlObj = new URL(url);

                conn = (HttpURLConnection) urlObj.openConnection();

                conn.setDoOutput(false);

                conn.setRequestMethod("GET");

                conn.setRequestProperty("Accept-Charset", charset);

                conn.setConnectTimeout(15000);

                conn.connect();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        try {
            //Receive the response from the server
            InputStream in = new BufferedInputStream(conn.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            String line;
            result = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

            Log.d("JSON Parser", "result: " + result.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }

        conn.disconnect();

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(result.toString());
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON Object
        return jObj;
    }
}

Result of drawing a route between two Markers:

這篇關于標記之間的 Google Maps API 和自定義折線路線的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why does the android emulator camera stop unexpectedly?(為什么android模擬器相機會意外停止?)
Android camera , onPictureTaken(byte[] imgData, Camera camera) method amp; PictureCallback never called(Android camera , onPictureTaken(byte[] imgData, Camera camera) 方法 amp;PictureCallback 從未調用過) - IT屋-程序員軟件開發技
Understanding the libGDX Projection Matrix(了解 libGDX 投影矩陣)
QR code reading with camera - Android(使用相機讀取二維碼 - Android)
IP camera with OpenCv in Java(Java中帶有OpenCv的IP攝像頭)
Android mock Camera(Android 模擬相機)
主站蜘蛛池模板: 亚洲免费视频在线观看 | 亚洲国产精品一区二区久久 | 特级丰满少妇一级aaaa爱毛片 | 天天天天操 | 精品国产免费人成在线观看 | 日韩成人在线观看 | 成人免费观看视频 | 欧美亚洲一区二区三区 | 成人欧美一区二区 | 一区二区三区四区五区在线视频 | 亚洲免费视频在线观看 | 欧美一区二区在线看 | 拍拍无遮挡人做人爱视频免费观看 | 成人在线一级片 | 国产精品久久久久久久久久久久 | 999久久久国产精品 欧美成人h版在线观看 | 一级黄色片毛片 | 成人精品国产一区二区4080 | 一级黄色夫妻生活 | 久久99久久99精品免视看婷婷 | 日韩视频精品在线 | 一级看片免费视频囗交动图 | 精品日韩一区二区 | 国产美女精品 | 2018国产精品 | 男女羞羞的网站 | 亚洲视频在线观看免费 | 精品一区二区三区在线观看国产 | 亚洲免费在线播放 | 亚洲a一区二区 | www视频在线观看 | 亚洲第一黄色网 | 国产91av视频在线观看 | 国产精品中文字幕在线播放 | 日韩欧美在线观看视频 | 免费h在线 | 国产成人综合在线 | 日韩视频一区二区在线 | 狠狠的干| 国产亚洲成av人在线观看导航 | 国产不卡视频 |