簡體   English   中英

谷歌地圖集群與方向

[英]Google map cluster with direction

我們有一個Google地圖庫來顯示群集中的多個標記。

我有兩個問題:

1.我們可以在Google地圖上顯示多個方向,如下圖所示嗎?

2.我們可以在聚類標記中顯示多個方向細節嗎?

在此輸入圖像描述

集群如下:

我將舉例說明:來自印度國家我已經在我的數據庫中保存了不同的方向。

喜歡

艾哈邁達巴德到德里

德里到阿格拉

艾哈邁達巴德要轟炸

傑伊普爾到德里

我必須顯示上述方向的集群取決於縮放級別,當用戶縮放谷歌地圖時,而不是群集,谷歌地圖上會顯示多個方向。

我想知道它是否可能? 如果是,那怎么樣?

您可以使用Directions API實現目標。 您提供起點和終點(可以是緯度/經度或地點名稱)。 另一個必填字段是行駛模式(默認駕駛)。

現在,方向返回頂點,頂點可以轉換為折線,然后在地圖上繪制。 這個答案非常好,我將使用它的代碼。

package com.example.simon.maps;

import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Document;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.util.SparseArray;

/**
 * Created by Simon on 2014 Jul 25.
 */

public class MainActivity extends FragmentActivity {

    final static String TAG = "MainActivity";
    GoogleMap mMap;
    GMapV2Direction md;
    int mZoomLevel;
    final float STARTING_ZOOM = 5.0f;
    // List of polylines for each zoom level
    SparseArray<List<Polyline>> mPolylines = new SparseArray<List<Polyline>>();

    public class direction {
        String start, end;
        int zoomLevel;

        direction(String pStart, String pEnd, int pZoomLevel) {
            start = pStart;
            end = pEnd;
            zoomLevel = pZoomLevel;
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy p = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(p);
        }

        md = new GMapV2Direction();
        mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

        List<direction> directions = new ArrayList<direction>();
        directions.add(new direction("Ahmedabad,Gujarat,India", "delhi,India", 4));
        directions.add(new direction("Ahmedabad,Gujarat,India","Bombay,Maharashtra,India", 4));
        directions.add(new direction("Jeypore,Odisha,India", "delhi,India", 5));
        for (MainActivity.direction direction : directions) {
            // Query
            Document doc = md.getDocument(direction.start, direction.end);
            // Parse the xml
            if (doc == null) {
                Log.e(TAG, "Failed to get the route from " + direction.start
                        + " to " + direction.end);
                continue;
            }
            // Get points
            ArrayList<LatLng> directionPoint = md.getDirection(doc);
            // Convert vertexes to a polyline
            PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
            for (LatLng aDirectionPoint : directionPoint) {
                rectLine.add(aDirectionPoint);
            }
            // Add poly to the map
            addPolyline(rectLine, direction.zoomLevel);
        }
        // Get the starting point of the first direction
        LatLng start = mPolylines.valueAt(0).get(0).getPoints().get(0);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(start, STARTING_ZOOM), 1000, null);
        // Set the initial zoom level and show the necessary polylines
        mZoomLevel = (int) STARTING_ZOOM;
        initPolylines(mZoomLevel);

        // Listen for the camera zoom level changes
        mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                // Note that because we are casting the zoomLevel to int,
                // it will be considered as changed only when it reaches
                // a new integer when rounded (e.g. 5.0, 6.0 etc.)
                int newZoomLevel = (int) cameraPosition.zoom;
                if (newZoomLevel != mZoomLevel) {
                    Log.d(TAG, "New zoom level: " + newZoomLevel);
                    // Loop all the changed zoom levels
                    // E.g. zoomed-out from 15 to 13, then hide [14, 15]
                    if (newZoomLevel < mZoomLevel) { // Zoomed out
                        for (int i=1; i<=mZoomLevel-newZoomLevel; i++)
                            hidePolylines(mPolylines.get(newZoomLevel+i));
                    } else { // Zoomed-in
                        for (int i=1; i<=newZoomLevel-mZoomLevel; i++)
                            showPolylines(mPolylines.get(mZoomLevel + i));
                    }
                    mZoomLevel = newZoomLevel;
                }
            }
        });
    }

    private void addPolyline(PolylineOptions polyOpts, int zoomLevel) {
        List<Polyline> polylines = mPolylines.get(zoomLevel);
        // Create polyline list for this zoom level, if it still doesn't exist
        if (polylines == null) {
            polylines = new ArrayList<Polyline>();
            mPolylines.put(zoomLevel, polylines);
        }
        // Append a new item to this poly list
        Polyline polyline = mMap.addPolyline(polyOpts);
        polyline.setVisible(false);
        polylines.add(polyline);
    }

    private void initPolylines(int zoomLevel) {
        for(int i=0; i<mPolylines.size(); i++) {
            // Loop until zoom level is reached
            if  (mPolylines.keyAt(i) > zoomLevel) break;

            showPolylines(mPolylines.get(mPolylines.keyAt(i)));
        }
    }

    private void showPolylines(List<Polyline> polylines) {
        if (polylines != null)
            for (Polyline polyline : polylines) polyline.setVisible(true);
    }

    private void hidePolylines(List<Polyline> polylines) {
        if (polylines != null)
            for (Polyline polyline : polylines) polyline.setVisible(false);
    }

}

此代碼將折線添加到SparseArray,該SparseArray保存每個縮放級別的折線。 默認情況下隱藏所有策略,僅在達到特定縮放級別時(也在initPolylines )顯示。

這里有幾點需要注意:

  1. 由於縮放可以同時改變幾個級別,我們循環每個級別以隱藏/顯示特定方向。
  2. 位置名稱必須精確或者找不到它們,您可能會在代碼中收到一堆錯誤(我只添加了一個Log.e)
  3. 如果你想創建標記 ,比如說方向的起點和終點,你可以使用下面的代碼來獲得坐標:

     List<LatLng> points = mPolylines.valueAt(0).get(0).getPoints(); LatLng start = points.get(0); LatLng end = points.get(points.size()-1); 
  4. 可以在特定縮放級別添加/刪除標記,就像在此處使用折線一樣。

這是xml解析器的代碼(GMapV2Direction):

package com.example.simon.maps;

import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.google.android.gms.maps.model.LatLng;

public class GMapV2Direction {

    public GMapV2Direction() { }

    public Document getDocument(String start, String end) {
        String url = "http://maps.googleapis.com/maps/api/directions/xml?"
                + "origin="+start+"&destination="+end+"&units=metric&mode=driving";
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse response = httpClient.execute(httpPost, localContext);
            InputStream in = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            return builder.parse(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public ArrayList<LatLng> getDirection (Document doc) {
        NodeList nl1, nl2, nl3;
        ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
        nl1 = doc.getElementsByTagName("step");
        if (nl1.getLength() > 0) {
            for (int i = 0; i < nl1.getLength(); i++) {
                Node node1 = nl1.item(i);
                nl2 = node1.getChildNodes();

                Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
                nl3 = locationNode.getChildNodes();
                Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
                double lat = Double.parseDouble(latNode.getTextContent());
                Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
                double lng = Double.parseDouble(lngNode.getTextContent());
                listGeopoints.add(new LatLng(lat, lng));

                locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
                nl3 = locationNode.getChildNodes();
                latNode = nl3.item(getNodeIndex(nl3, "points"));
                ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
                for (LatLng anArr : arr) {
                    listGeopoints.add(new LatLng(anArr.latitude, anArr.longitude));
                }

                locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
                nl3 = locationNode.getChildNodes();
                latNode = nl3.item(getNodeIndex(nl3, "lat"));
                lat = Double.parseDouble(latNode.getTextContent());
                lngNode = nl3.item(getNodeIndex(nl3, "lng"));
                lng = Double.parseDouble(lngNode.getTextContent());
                listGeopoints.add(new LatLng(lat, lng));
            }
        }

        return listGeopoints;
    }

    private int getNodeIndex(NodeList nl, String nodename) {
        for(int i = 0 ; i < nl.getLength() ; i++) {
            if(nl.item(i).getNodeName().equals(nodename))
                return i;
        }
        return -1;
    }

    private ArrayList<LatLng> decodePoly(String encoded) {
        ArrayList<LatLng> poly = new ArrayList<LatLng>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;
        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;
            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
            poly.add(position);
        }
        return poly;
    }
}

最后的一些說明:

  • 如果你有很多方向將它們加載到initPolylinesshowPolylines並在hidePolylines卸載它們
  • 如果要在地圖上顯示的方向不變,最好的方法是在特定縮放級別上使用具有折線的切片。 一個很好的免費工具是Maperitive ,它可以將瓷磚導出到你想要的任意數量的縮放級別。 然后,您將在線存儲圖塊並使用UrlTileProvider在地圖上顯示它們。

我不知道我是否正確理解了你的問題,但無論如何我都會試着回答。 您可以創建多條折線,並從Google地圖方向API的JSON響應添加到地圖。 這樣,您可以在單個地圖中添加多個路線。

據我所知,第二個問題是,您希望群集標記提供您正在繪制的路線的詳細信息。 為此,我猜你可以對API返回的點進行聚類,並使用自定義標記簇來顯示所需的信息。

希望能幫助到你

-Edit-如果您想在Android應用程序中繪制路線,則不會獲得任何可以使用的DirectionRenderer。 您需要使用折線渲染路線。 這就是我在上面的答案中提到創建折線的原因。 作為對Direction請求的響應,您將獲得一個包含多個坐標點的JSON,這些坐標點可以通過繪制折線來連接。 您可以在地圖上(從任何點到任何點)擁有多個此類折線。 案例1:您想要從Bombay到Goa的2條路線,將alternative參數設置為true。 案例2:你想要兩條不同的路線,班加羅爾到孟買,果阿到德里,向API發出兩個不同的請求。 您將獲得兩條不同的路線並在地圖上繪制它們。

對於第二個問題,由於沒有內置的路徑聚類(折線),您必須編寫顯示在地圖中的聚類圖標的邏輯。 要獲取當前縮放級別,請使用以下命令。

GoogleMap map;    
float zoom = map.getCameraPosition().zoom;

用於顯示和隱藏折線

Polyline line = map.addPolyline(new PolylineOptions()
 .add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))

 // multiple points that you get from the response of directions API

 .width(5)
 .color(Color.RED));

//your logic for visibility
if(zoom<=5){
  line.setVisible(true);
}

我在API中找不到任何方法來使用代碼隱藏群集圖標。 我猜它應該在某個縮放級別后隱藏自己。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM