2017-12-20 17 views
-1

私は、経度と緯度に基づいてログインしたユーザーの位置を動的に表示するアンドロイドアプリを作成したいと考えています。動的には、マップマーカーのユーザー数は決して固定されません。ユーザーの数を変更することができます。また、ユーザー情報はfirebaseデータベースに格納され、そこから現在のユーザー数が計算され、マーカ付きのGoogleマップに表示されます。これどうやってするの ??Google Mapで動的に複数の位置にマーカーを追加

+0

ない先生を表示します(options) '、optionsはMarkerOptionsクラスのオブジェクトであり、optionsは経度、緯度を一度に1つだけ取ります。私の質問は、これを使って複数のユーザーの位置を動的に追加する方法でした。 –

答えて

0

セッションオブジェクトにユーザーの経度と緯度を格納できます。

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

アプリケーションの管理モードでは、すべてのアクティブなセッションにアクセスできます。

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で上記のリスナーを定義

使用HttpSessionActivationListenerすべてのアクティブなセッションを見つけるために、アクティブなセッションを取得するためのコードの下

<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. 

すべてのアクティブセッションを通過し、経度と緯度を格納します。あなたがアンドロイドでのGoogleマップで作業する場合

コードは、あなたがgoogleMap.addMarker」として知られている方法があることを知っている必要があります実際には、複数のマーカー

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

}

+0

ありがとうございます。私は新しい学習者であるため、ビデオチュートリアルを私に提供してください。 –

関連する問題