5 android polyline android-maps-v2 android-maps-utils
我polyline在地图上绘图,现在需要向用户显示一些数据。
如何在每个上绘制文本或InfoWindow polyline?
我添加polyline像:
ArrayList<LatLng> points = null;
PolylineOptions lineOptions = null;
MarkerOptions markerOptions = new MarkerOptions();
// Traversing through all the routes
for(int i=0;i<result.size();i++){
points = new ArrayList<LatLng>();
lineOptions = new PolylineOptions();
String color = colors[i % colors.length];
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(5);
lineOptions.color(Color.parseColor(color));
// Drawing polyline in the Google Map for the i-th route
mMap.addPolyline(lineOptions);
}
Run Code Online (Sandbox Code Playgroud)
例如,我需要这样做:
我通过在折线上创建一个不可见的标记,然后显示其信息窗口来实现此目的。例如:
//use a transparent 1px & 1px box as your marker
BitmapDescriptor transparent = BitmapDescriptorFactory.fromResource(R.drawable.transparent);
MarkerOptions options = new MarkerOptions()
.position(new LatLng(someLatitide, someLongitude))
.title(someTitle)
.snippet(someSnippet)
.icon(transparent)
.anchor((float) 0.5, (float) 0.5); //puts the info window on the polyline
Marker marker = mMap.addMarker(options);
//open the marker's info window
marker.showInfoWindow();
Run Code Online (Sandbox Code Playgroud)
更新以包括触摸折线和打开信息窗口的一般方法: 1. 实现 OnMapClickListener 2. 在 onMapClick 事件中,确定用户是否触摸了折线。我通过将折线点存储在四叉树中并在四叉树中搜索距用户触摸屏幕最近的点来实现此目的。如果距离在特定阈值内(即接近折线),则创建上面引用的不可见标记并打开其信息窗口。如果触摸不在阈值内,则忽略 onMapClick 事件。3. 在下一个 onMapClick 事件中,删除之前创建的不可见标记,这样就不会出现一堆占用内存的不可见标记。希望有帮助。