2017-03-11 19 views
1

Maps APIを使用するAndroidスタジオでアプリケーションを作成するのに忙しいです。
GoogleマップにOnInfoWindowClickListenerを正常に作成しました。 Googleマップに設定した2種類のマーカーがありますが、最終的には何千種類ものマーカーを設定したいと考えています。
ユーザーが情報ウィンドウをクリックすると、新しいアクティビティを開く必要があります。

例として、ユーザーが「マーカーA」をクリックすると、「活動A」が開く必要があります。または、ユーザーが「マーカーB」をクリックすると、「活動B」が開く必要があります。または、ユーザーが「マーカーC」をクリックすると、「アクティビティーC」が開く必要があります。

自分のコードで 'if statement & else if statement'を使用しようとしましたが、Marker's Info Windowをクリックしても何も起こりません。
OnInfoWindowClickに応じて異なるアクティビティを開く方法

package com.msp.googlemapsproject; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MainActivity extends Activity { 

    static final LatLng CapeTown = new LatLng(-29.759933, 30.801030); 
    static final LatLng Durban = new LatLng(-29.858680, 31.021840); 
    private GoogleMap googleMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     try{ 

      if (googleMap == null) { 

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

      } 

      googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
      googleMap.setMyLocationEnabled(true); 
      googleMap.setTrafficEnabled(true); 
      googleMap.setIndoorEnabled(true); 
      googleMap.setBuildingsEnabled(true); 
      googleMap.getUiSettings().setZoomControlsEnabled(true); 

      final Marker marker_CapeTown = googleMap.addMarker(new MarkerOptions().position(CapeTown).title("Cape Town")); 
      final Marker marker_Durban = googleMap.addMarker(new MarkerOptions().position(Durban).title("Durban")); 

      googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { 

       @Override 
       public void onInfoWindowClick(Marker marker) { 
        if (googleMap.equals("Durban")) { 
         Intent intent = new Intent(MainActivity.this, Durban.class); 
         startActivity(intent); 
        } 

        else if (googleMap.equals("Cape Town")) { 
         Intent intent = new Intent(MainActivity.this, CapeTown.class); 
         startActivity(intent); 
        } 
       } 
      }); 

     } catch (Exception e) { 

      e.printStackTrace(); 
     } 
    } 
} 

答えて

2

あなたのif節に次のようにチェックされています

if (googleMap.equals("Durban")) { 
     Intent intent = new Intent(MainActivity.this, Durban.class); 
     startActivity(intent); 
} 

いますが、public void onInfoWindowClick(Marker marker)方法のマーカーを確認する必要があります。

このような何か:

if (marker.getTitle().equals("Durban")) { 
     Intent intent = new Intent(MainActivity.this, Durban.class); 
     startActivity(intent); 
} 
+0

をありがとうございました。過去数週間、私はオンラインで答えを探してきました。ありがとうございました!コードが動作します! – MSP

関連する問題