在Google地图中动态添加多个位置的标记

时间:2017-12-20 08:17:31

标签: android

我想创建一个Android应用程序,它将根据动态的经度和纬度显示已记录的用户位置。动态意味着,我永远不会修复地图标记的用户数量。用户数量可以变化。用户信息将存储到firebase数据库中,应用程序计算当前用户的数量,并使用标记在谷歌地图上显示它们。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以将用户的经度和纬度存储在会话对象中。

HttpSession session = request.getSession();
session.setAttribute("longitude", longitude);
session.setAttribute("latitude", latitude);

在应用程序的管理员模式中,您可以访问所有活动会话。

使用HttpSessionActivationListener查找所有活动会话

class SessionCounterListener implements HttpSessionActivationListener
{

    public static final Map activeSessions = HashMap<String,
    HttpSession>();

public void sessionDidActivate(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    activeSessions.put(session.getId(), session);
}

public void sessionWillPassivate(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    activeSessions.remove(session.getId();
}

}

将web.xml中的上述侦听器定义为

<listener>
<listener-class>my.package.SessionCounterListener</listener-class>
</listener>

使用以下代码获取活动会话

SessionCounterListener.activeSessions.size(); // Returns the number of active sessions.

SessionCounterListener.activeSessions.getValues(); // Returns the all the active sessions.

遍历所有活动会话并存储经度和纬度。

显示多个标记的代码

ArrayList<MarkerData> markersArray = new ArrayList<MarkerData>();

for(int i = 0 ; i < markersArray.size() ; i++ ) {

    createMarker(markersArray.get(i).getLatitude(), markersArray.get(i).getLongitude(), markersArray.get(i).getTitle(), markersArray.get(i).getSnippet(), markersArray.get(i).getIconResID());
}

...

protected Marker createMarker(double latitude, double longitude, String title, String snippet, int iconResID) {

    return googleMap.addMarker(new MarkerOptions()
        .position(new LatLng(latitude, longitude))
        .anchor(0.5f, 0.5f)
        .title(title)
        .snippet(snippet);
        .icon(BitmapDescriptorFactory.fromResource(iconResID)));

}