Google Map Android API v2-折线上的InfoWindow? [英] Google map Android API v2 - InfoWindow on polyline?

查看:168
本文介绍了Google Map Android API v2-折线上的InfoWindow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在地图上绘制polyline,我现在需要向用户显示一些数据.

I am drawing polyline on my map,I need to show some data to user now.

如何在每个polyline上绘制文本或InfoWindow?

How I can draw text or InfoWindow on each 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);
}

例如,我需要这样做:

推荐答案

我可以通过在折线上创建一个不可见的标记,然后显示其信息窗口来做到这一点.例如:

I do this by creating an invisible marker on the polyline and then showing its info window. For example:

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

更新为包括触摸折线和打开信息窗口的一般方法: 1.实现一个OnMapClickListener 2.在onMapClick事件上,确定用户是否触摸了折线.为此,我将折线点存储在四叉树中,并在四叉树中搜索用户触摸屏幕的最近点.如果距离在某个阈值内(即接近折线),请创建上面引用的不可见标记,然后打开其信息窗口.如果触摸不在阈值之内,则忽略onMapClick事件. 3.在下一个onMapClick事件上,删除先前创建的不可见标记,这样就不会有很多不可见标记占用内存.希望有帮助.

Update to include general approach to touching the polyline and opening an info window: 1. Implement an OnMapClickListener 2. On an onMapClick event, determine whether the user has touched the polyline. I do this by storing the polyline points in a quadtree and searching the quadtree for the closest point from where the user touched the screen. If the distance is within a certain threshold (i.e. close to the polyline), create the invisible marker referenced above and open its info window. If the touch is not within the threshold, ignore the onMapClick event. 3. On the next onMapClick event delete the previously created invisible marker so you do not have a bunch of invisible markers taking up memory. Hope that helps.

这篇关于Google Map Android API v2-折线上的InfoWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆