- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我们有一个 Google map library在集群中显示多个标记。
我有两个问题:
集群如下:
我会给你举个例子:我在我的数据库中保存了来自印度的不同方向。
喜欢
艾哈迈达巴德到德里
德里到阿格拉
艾哈迈达巴德到孟买
斋浦尔到德里
我必须根据缩放级别显示上述方向的集群,当用户缩放谷歌地图时,谷歌地图上显示的不是集群,而是多个方向。
我想知道这是否可行?如果是那么怎么办?
最佳答案
您可以使用 Directions API 实现您的目标。您提供起点和终点(可以是纬度/经度或地点名称)。另一个必填字段是出行方式(默认驾驶)。
现在 Directions 返回可以转换为折线然后绘制在 map 上的顶点。 This answer做得很好,我将使用它的代码。
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 中,该数组包含每个缩放级别的多段线。默认情况下,所有多边形都是隐藏的,只有在达到特定缩放级别时才会显示(也在 initPolylines
)。
这里有几点需要注意:
如果您想在(比方说方向的起点和终点)创建标记,您可以使用以下代码获取坐标:
List<LatLng> points = mPolylines.valueAt(0).get(0).getPoints();
LatLng start = points.get(0);
LatLng end = points.get(points.size()-1);
这是 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;
}
}
一些最后的说明:
initPolylines
和 showPolylines
中,然后将它们卸载到 hidePolylines
关于android - 带方向的谷歌地图集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24646864/
运行 PostgreSQL(7.4 和 8.x),我认为这是可行的,但现在我遇到了错误。 我可以单独运行查询,它工作得很好,但如果我使用 UNION 或 UNION ALL,它会抛出错误。 这个错误:
我试图为我的应用程序创建一个导航,使用抽屉导航我的 fragment 之一(HomeFragment)有一个 ViewPager,可容纳 3 个 fragment (Bundy Clock、Annou
以我目前正在开发的应用为例: - 它有一个包含多个项目的抽屉导航;现在有两个项目让我感兴趣,我将它们称为 X 和 Y。 X 和 Y 都在单击时显示包含 x 元素或 y 元素列表的 fragment 选
我有一个形状为 (370,275,210) 的 NumPy 数组,我想将其重新整形为 (275,210,370)。我将如何在 Python 中实现这一点? 370是波段数,275是行数,210是图像包
我们如何与被子 UIViewController 阻止的父 UIViewController(具有按钮)交互。显然,触摸事件不会通过子 Nib 。 (启用用户交互) 注意:我正在加载默认和自定义 NI
我是 Jpa 新手,我想执行过程 我的代码如下 private static final String PERSISTENCE_UNIT_NAME = "todos"; private static
与安装了 LAMP 的 GCE 相比,选择与 Google Cloud SQL 链接的 GCE 实例有哪些优势? 我确定 GCE 是可扩展的,但是安装在其上的 mysql 数据库的可扩展性如何? 使用
这个问题在这里已经有了答案: Value receiver vs. pointer receiver (3 个答案) 关闭 3 年前。 我刚接触 golang。只是想了解为 Calc 类型声明的两种
我不小心按了一个快捷键,一个非常漂亮的断线出现在日期上。 有点像 # 23 Jun 2010 -------------------- 有人知道有问题的快捷方式吗?? (我在 mac 上工作!) 在
我正在Scala中编写正则表达式 val regex = "^foo.*$".r 这很好,但是如果我想做 var x = "foo" val regex = s"""^$x.*$""".r 现在我们有
以下 XML 文档在技术上是否相同? James Dean 19 和: James Dean 19 最佳答案 这两个文档在语义上是相同的。在 X
我在对数据帧列表运行稳健的线性回归模型(使用 MASS 库中的 rlm)时遇到问题。 可重现的示例: var1 <- c(1:100) var2 <- var1*var1 df1 <- data.f
好的,我有一个自定义数字键盘,可以在标签(numberField)中将数字显示为 0.00,现在我需要它显示 $0.00。 NSString *digit = sender.currentTitle;
在基于文档的应用程序中,使用 XIB 文件,创建新窗口时其行为是: 根据最后一个事件的位置进行定位和调整大小 window 。 如果最后一个事件窗口仍然可见,则新窗口 窗口应该是级联的,这样它就不会直
我想使用参数进行查询,如下所示: SELECT * FROM MATABLE WHERE MT_ID IN (368134, 181956) 所以我考虑一下 SELECT * FROM MATABLE
我遇到一些性能问题。 我有一个大约有 200 万行的表。 CREATE TABLE [dbo].[M8]( [M8_ID] [int] IDENTITY(1,1) NOT NULL,
我在 jquery 中的按键功能遇到问题。我不知道为什么按键功能不起作用。我已经使用了正确的 key 代码。在我的函数中有 2 个代码,其中包含 2 个事件键,按一个键表示 (+) 代码 107 和(
我想显示音频波形,我得到了此代码,它需要.raw音频输入并显示音频波形,但是当我放入.3gp,.mp3音频时,我得到白噪声,有人可以帮助我如何使其按需与.3gp一起使用使用.3gp音频运行它。 Inp
我无法让 stristr 函数返回真值,我相信这是因为我的搜索中有一个 $ 字符。 当我这样做时: var_dump($nopricecart); 完整的 $nopricecart 值是 $0 ,我得
如果我有这样的循环: for(int i=0;i O(n) 次。所以do some执行了O(n)次。如果做某事是线性时间,那么代码片段的复杂度是O(n^2)。 关于algorithm - 带 If 语
我是一名优秀的程序员,十分优秀!