标记之间的 Google Maps API 和自定义折线路线

Posted

技术标签:

【中文标题】标记之间的 Google Maps API 和自定义折线路线【英文标题】:Google Maps API and custom polyline route between Markers 【发布时间】:2016-01-01 11:23:02 【问题描述】:

我想为一个安卓应用创建一个自定义路由,我不确定我应该使用哪个 API 以及它是否与 Java 兼容。

据我所知,我需要使用航路点来制定路线(我不需要知道两点之间的距离,只是为了制定路线)。

目标是从地图一侧的菜单中选择一个选项,并显示两个标记之间的自定义路线之一。

【问题讨论】:

【参考方案1】:

您可以使用Google Maps API v2 for android 和Google Maps Directions webservice API 来做到这一点

对于开始使用 Google Maps API,已经有很多其他好的答案了。 See here 获取简单地图活动的完整工作示例。请注意,您还需要设置一个 API 密钥才能用于您的项目。

关于使用 Google Maps Directions webservice API,您应该首先阅读文档。您可以使用 API 密钥并在开发者控制台中启用该 API,但目前它仍然可以在不使用 API 密钥的情况下工作。

这是您使用 Google Maps API 在两点之间绘制折线所需的基本代码,请注意,从 API 返回的点被编码为需要解码的 base 64 编码字符串。

首先,确保您的项目包含 Google Maps Utility 库,该库将用于解码 base64 编码的折线:

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

这里是 AsyncTask,调用它时你应该给它两个 LatLng 点。

您可以使用两个 LatLng 对象调用 AsyncTask,例如在两个标记之间:

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

这里是 AsyncTask 代码:

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);

    

AsyncTask 引用了 Activity 的一些成员变量,即 Polyline 和 GoogleMap,Activity 定义如下所示:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback

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

这是本示例中使用的 JSONParser 类,请注意这是针对 android-23 更新的修改版本,我 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;
    

在两个标记之间绘制路线的结果:

【讨论】:

如何添加第三个标记并继续前往第三个标记? @AndrewJeromeBernard 您只需添加第三个标记,然后绘制路线:new GetDirectionsAsync().execute(markerTwo.getPosition(), markerThree.getPosition());

以上是关于标记之间的 Google Maps API 和自定义折线路线的主要内容,如果未能解决你的问题,请参考以下文章

带标签的 Google Maps API v3 标记

在两个活动之间使用相机图像处理意图并将其传递给 Google Maps API 上的标记不起作用

是否可以在 Google Maps API v3 上编写自定义文本?

在 google maps api v3 中隐藏方向服务中的标记

在 Google Maps api v2 中设置自定义 InfoWindow 的宽度

在 Kotlin 上为 Google Maps Api 创建动态标记和变量访问