簡體   English   中英

Android谷歌地圖V2 - OnInfoWindow點擊幾個標記

[英]Android Google Maps V2 - OnInfoWindowClick on several markers

當你點擊infowindow時,我有一個代碼讓一個標記啟動一個活動。 它工作得非常好。 但是當我嘗試添加另一個標記和另一個@override時,它總是在所有Markers infowindows上打開最后一個類。 因此,實質上所有標記infowindows在單擊時打開相同的活動,而不是打開我打算使用的單獨類。

這是下面的代碼,它在InfoWindowClicked上成功打開了1個活動。 我把它叫做example.class, 這適用於需要這個例子的每個人

 public class MainActivity extends Activity implements OnInfoWindowClickListener {

 private GoogleMap googlemap;


 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

if(isGooglePlay()){
setContentView(R.layout.activity_main);
setUpMap();

{    }    }


googlemap.addMarker(new MarkerOptions()
.position(new LatLng(0,-0))
.title("Title")
.snippet("Snippet")    
.icon(BitmapDescriptorFactory.fromResource(R.drawable.star)));
googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

@Override
public void onInfoWindowClick(Marker marker) {
 Intent intent = new Intent(MainActivity.this,Example.class);
 startActivity(intent);
          } });
{

因此,在谷歌地圖googlemap / mMap (或你稱之為你的任何東西)和@override void Oncreate (我的應用程序僅在GooglePlayServices可用時啟動,你的應用程序可能不是這樣)下面你可以放置標記和infowindowclick代碼。

確保代碼中的某處還有(通常在私有的void setUpMap(){)

    googlemap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

現在下面是帶有兩個標記的代碼,但是當它們被單擊時它們都會打開example2.class。 有人可以幫我解決這個問題,所以我可以將它們分開並讓它們打開不同的類嗎?

 googlemap.addMarker(new MarkerOptions()
.position(new LatLng(0,-0))
.title("Title")
.snippet("Snippet")    
.icon(BitmapDescriptorFactory.fromResource(R.drawable.star)));
googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

@Override
public void onInfoWindowClick(Marker marker) {
 Intent intent = new Intent(MainActivity.this,Example.class);
 startActivity(intent);
          } });
{

      {
googlemap.addMarker(new MarkerOptions()
.position(new LatLng(  0, -0))
.title("Title")
.snippet("Snippet")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.star)));
googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {


@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(MainActivity.this,Example2.class);
startActivity(intent);
        }  });
}}


}

編輯(07/06/2013):

private GoogleMap googlemap; 
private Map<Marker, Class> allMarkersMap = new HashMap<Marker, Class>();

以上是在班級^^^

 Marker marker1 = googlemap.addMarker(new MarkerOptions()
.position(new LatLng(0,0))
.title("England")
.snippet("London")    
.icon(BitmapDescriptorFactory.fromResource(R.drawable.star)));
allMarkersMap.put(marker1, Contact.class);

}

public void onInfoWindowClick(Marker marker) {
Class cls = allMarkersMap.get(marker);
Intent intent = new Intent(MainActivity.this, cls);
startActivity(intent);

}

上面的^^^^在我的“protected void onCreate(Bundle savedInstanceState){”之下。 沒有錯誤,當我調試時我可以看到Marker但無法點擊InfoWindow。 警告是:

Class is a raw type. References to generic type Class<T> should be parameterized    

我在類級別上看到了這個警告兩次,並且在公共虛空中看到了一次onInfoWindow上的單詞'Class' 我試着像“添加類型參數的‘類’一些不同的東西,但它並沒有在公共無效我改變了標記標記1和下面allMarkersMap.get(標記)線工作在標記標記 ;改變(標記)(marker1)只是嘗試但它沒有用。還有什么我可以做的嘗試初始化onInfoWindowClick函數?

從MaciejGórski的幫助中,當您在GoogleMapsV2上單擊單獨的Marker infowindows時,可以打開不同類(活動,例如頁面)的示例:

將此添加到您的GoogleMap課程級別:

private GoogleMap googlemap/mMap (or whatever you call yours); 
private Map<Marker, Class> allMarkersMap = new HashMap<Marker, Class>();

在protected void onCreate(Bundle savedInstanceState)下面{super.onCreate(savedInstanceState); 放入你的標記:

    Marker marker = googlemap.addMarker(new MarkerOptions()
    .position(new LatLng(0,-0))
    .title("London")
    .snippet("North London")    
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.star)));
    allMarkersMap.put(marker, NorthLondon.class);
    googlemap.setOnInfoWindowClickListener(this);



    Marker marker1 = googlemap.addMarker(new MarkerOptions()
    .position(new LatLng(0244534,-1232312))
    .title("USA")
    .snippet("Washington")    
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.star)));
    allMarkersMap.put(marker1, Washington.class);
    googlemap.setOnInfoWindowClickListener(this);


    Marker marker2 = googlemap.addMarker(new MarkerOptions()
    .position(new LatLng(42343244,-0.334322))
    .title("Italy")
    .snippet("Rome"));
    allMarkersMap.put(marker2, Rome.class);
    googlemap.setOnInfoWindowClickListener(this);

    }

    public void onInfoWindowClick(Marker marker) {
    Class cls = allMarkersMap.get(marker);
    Intent intent = new Intent(MainActivity.this, cls);
    startActivity(intent);

}

以上是單獨的標記。 如果我要創建另一個,我會稱之為Marker marker3,然后是4,5等...它在allMarkersMap.put(marker,.class)中要求.class; 輸入你想要的課程,這樣就可以打開你想要的課程。 公共無效的OnInfoWindowClick代碼下面的標記代碼,這是回調。

就是這樣。 當您在標記中單擊InfoWindows時,他們應該打開您在MarkerOptions代碼中放置的活動類!

歸功於MaciejGórski

setOnInfoWindowClickListener setsetOnInfoWindowClickListener意味着它將覆蓋之前set任何值。 此功能在GoogleMap對象上調用,因為有一個GoogleMap對象,所以有一個活動的OnInfoWindowClickListener

你使用它的方式是使用if elseswitch或者Map<Marker, Class>來決定基於回調onInfoWindowClick(Marker marker)的參數會發生什么:

public void onInfoWindowClick(Marker marker) {
    Class cls = map.get(marker);
    Intent intent = new Intent(MainActivity.this, cls);
    startActivity(intent);
}

當然,您需要先提前初始化此地圖:

Marker marker1 = googlemap.addMarker...
map.put(marker1, Example.class);

編輯:

// on the class level:
private Map<Marker, Class> allMarkersMap = new HashMap<Marker, Class>();

// in the onCreate or elsewhere
Marker marker1 = googlemap.addMarker(new MarkerOptions()
    .position(new LatLng(0,-0))
    .title("Netherlands")
    .snippet("Amsterdam")    
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.star)));
allMarkersMap.put(marker1, Example.class);

// callback
public void onInfoWindowClick(Marker marker) {
    Class cls = allMarkersMap.get(marker);
    Intent intent = new Intent(MainActivity.this, cls);
    startActivity(intent);
}

對於問題:

Class is a raw type. References to generic type Class<T> should be parameterized  

我添加<? >在課堂旁邊:

private Map<Marker, Class<?>> allMarkersMap = new HashMap<Marker, Class<?>>();

Class<?> cls = allMarkersMap.get(marker);

如果您已經在片段類中工作(就像發生在我身上),您將改變:

public void onInfoWindowClick(Marker marker) {
    Class<?> cls = allMarkersMap.get(marker);
    Intent intent = new Intent(getActivity(), cls);
    startActivity(intent);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM