2013-03-07 77 views
1

我有這個問題:google maps api v2 - 開始活動表單標記

我需要從地圖上點擊一個標記,在片段上可視化時開始一個活動;

這是一件我的XML的:

<LinearLayout 
    android:id="@+id/mylayout" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="560" >    

<fragment 
android:id="@+id/mapview" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_margin="5dp" 
android:clickable="true" 
android:name="com.google.android.gms.maps.MapFragment" 
/> 

</LinearLayout> 

我的活動1桅杆呼叫活性2(這裏不寫),當使用自來水一個標誌 建立了基於Tabella數據:

@SuppressLint("NewApi") 
public class ACTIVITY1 extends Activity { 
. . . . . . 

ArrayList<HashMap<String, String>> Tabella(); 
. . . . . . 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.mylayout); 

mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapview)).getMap(); 
    . . . . 
    showmap(null); 
    . . . . 
} 


//prepare markers on map fragment 
private void showMap(OnMarkerClickListener OnMarkerClickListener) { 

    arrM = new ArrayList<String>(); 

    mapView.setOnMarkerClickListener (OnMarkerClickListener); 

    try { 

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

       HashMap<String, String> a = Tabella.get(i); 
       final String insegna = a.get("insegna"); 
       String lat1 = a.get("latitudine"); 
       String longi = a.get("longitudine"); 

       if (lat1.equals("")) lat1 = "0.0"; 
       if (longi.equals("")) longi = "0.0"; 
       latitude = Double.parseDouble(lat1); 
       longitude = Double.parseDouble(longi); 

       cameraPosition = new CameraPosition.Builder() 
           .target(new LatLng(latitude,longitude))  
           .zoom(17)     // Sets the zoom 
           .build();     

       marker = mapView.addMarker(new MarkerOptions() 
         .position(new LatLng(latitude, longitude)) 
         .snippet("") 
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.iconpoi)) 
         .title(insegna)); 

       mapView.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
       marker.showInfoWindow(); 

       arrM.add(marker.getId()); 

       //mapView.setOnMarkerClickListener (OnMarkerClickListener); 

      } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     } 

} 

//called when marker selected 
public void onMarkerClick(Marker marker) { 
    . . . 
    Intent myIntent = new Intent(ACTIVITY1 .this,Activity2.class); 
    . . 
    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    ACTIVITY1 .this.startActivity(myIntent); 
    . . 

} 

} 

爲什麼公衆void onMarkerClick(Marker marker)未被調用?

出了什麼問題?

感謝

回答

0

爲什麼公共無效onMarkerClick(標記標記)不叫?

因爲你叫showmap(null)因此從未註冊過監聽器。

+0

那麼哪個是正確的代碼? – user1812637 2013-03-07 17:00:08

+0

@ user1812637:沒有證據表明您已經編寫了「正確的代碼」。當然,根據你當前的實現,你不能將'null'傳遞給'showmap()'。你需要傳遞一個實際的'OnMarkerClickListener'對象。你從哪裏得到這些信息取決於你。以下是演示使用OnMarkerClickListener的示例項目:https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Taps – CommonsWare 2013-03-07 17:05:17