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

问题描述 投票:0回答:1

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

android
1个回答
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)));

}

© www.soinside.com 2019 - 2024. All rights reserved.