2013-12-17 191 views
1

在我的應用程序中,我繪製了12個標記到地圖,然後爲每個標記我有12個dilalog框彈出並顯示標記位置的地址。我一直在想這樣做的一個更好的方法,所以我一直試圖創建一個列表來保存所有的標記,並在點擊時將列表中的標記從對話框中傳遞到對話框中,所以我不必12分隔對話框。優化Android代碼

目前我的標記正在繪製到屏幕上,但對話框不顯示何時標記被粘貼,但它顯示當我使用12個獨立的對話框。任何人都可以指出問題出在哪裏的正確方向。

private List<Marker> markers = new ArrayList<Marker>(); 


//sets the default map location to glasgow 
double defaultLat = 55.85432829452839; 
double defaultLng = -4.268357989501965; 

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

    //get drawable IDs 
    icoUser = R.drawable.yellow_point;   
    icoScotstoun = R.drawable.squash; 
    icoIbrox = R.drawable.rugby; 
    icoSecc = R.drawable.boxing; 
    icoKelvingrove = R.drawable.bowls; 
    icoHockey = R.drawable.hockey; 
    icoVeledrome = R.drawable.athetics; 
    icoCeltic = R.drawable.party;   
    icoTollcross = R.drawable.aqua;   
    icoHampden = R.drawable.athetics;   
    icoCathkin = R.drawable.cycleing; 
    icoStrathclyde = R.drawable.tri;  
    icoEdinburgh = R.drawable.athetics;  
    icoDundee = R.drawable.shooting;   


    //ArrayAdapter <String> adapter = new ArrayAdapter<String>(MucMainActivity.this, android.R.layout.simple_spinner_item, paths); 
    ArrayAdapter <String> adapter = new ArrayAdapter<String>(MucMainActivity.this, R.layout.spinner_layout, maptypes); 
    mapTypeSpinner = (Spinner) findViewById(R.id.spinner1); 
    mapTypeSpinner.setAdapter(adapter);  
    mapTypeSpinner.setOnItemSelectedListener(this);  

    //checks if the map has been instantiated or not if it hasn't then the map gets istantiated 
    if(mMap == null) 
    { 
     //passes the map fragment ID from the layout XML and casts it to a map fragment object 
     //and gets the google map object. 
     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
    } 

    if(mMap != null) 
    { 
     defaultSetting();   
     addListenerOnChkIos(); 
     mMap.setOnMarkerClickListener(this); 
    } 


    //showUserLocation();   

} 


//@Override 
// public boolean onCreateOptionsMenu(Menu menu) 
//{ 
    // Inflate the menu; this adds items to the action bar if it is present. 
    //getMenuInflater().inflate(R.menu.muc_main, menu); 
    //return true; 
//} 


@Override 
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
{ 
    // TODO Auto-generated method stub 
    int position = mapTypeSpinner.getSelectedItemPosition(); 
    switch(position) 
    { 
    case 0: 
     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

     break; 

    case 1: 
     mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
     break; 

    case 2: 
     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
     break; 

    } 
} 


@Override 
public void onNothingSelected(AdapterView<?> arg0) 
{ 
    // TODO Auto-generated method stub 

} 

private void showUserLocation() 
{ 
    //get location manager 
      locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
      //get last location 
      Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      double lat = lastLoc.getLatitude(); 
      double lng = lastLoc.getLongitude(); 
      //create LatLng 
      LatLng lastLatLng = new LatLng(lat, lng); 

      //remove any existing marker 
      if(marUser!=null) marUser.remove(); 
      //create and set marker properties 
      marUser = mMap.addMarker(new MarkerOptions() 
      .position(lastLatLng) 
      .title("You are here") 
      .icon(BitmapDescriptorFactory.fromResource(icoUser))); 
      //move to location 
      mMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 1000, null);  

} 

public void defaultSetting() 
{ 

    //sets the map type 
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    // sets the default location of the map to Glasgow 
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(defaultLat, defaultLng), 12.0f), null); 
    //mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(55.85432829452839, -4.268357989501965)), 3000, null);  
    //mMap.addMarker(new MarkerOptions().position(new LatLng(defaultLat,defaultLng)).title("hello")); 
    addMarkers(); 


} 

public void addMarkers() 
{ 
    for (int i = 0; i < 11; i++) 
    { 
     red = mMap.addMarker(new MarkerOptions().position(new LatLng(55.88069,-4.34025)).icon(BitmapDescriptorFactory.fromResource(icoScotstoun))); 
     markers.add(red); 
     blue = mMap.addMarker(new MarkerOptions().position(new LatLng(55.85360,-4.30454)).icon(BitmapDescriptorFactory.fromResource(icoIbrox))); 
     markers.add(blue); 
     green = mMap.addMarker(new MarkerOptions().position(new LatLng(55.86070,-4.28761)).icon(BitmapDescriptorFactory.fromResource(icoSecc))); 
     markers.add(green); 
     yellow = mMap.addMarker(new MarkerOptions().position(new LatLng(55.86782,-4.28875)).icon(BitmapDescriptorFactory.fromResource(icoKelvingrove))); 
     markers.add(yellow); 
     brown = mMap.addMarker(new MarkerOptions().position(new LatLng(55.84496,-4.23671)).icon(BitmapDescriptorFactory.fromResource(icoHockey))); 
     markers.add(brown); 
     pink = mMap.addMarker(new MarkerOptions().position(new LatLng(55.84496,-4.23671)).icon(BitmapDescriptorFactory.fromResource(icoVeledrome))); 
     markers.add(pink); 
     purple = mMap.addMarker(new MarkerOptions().position(new LatLng(55.84959,-4.20555)).icon(BitmapDescriptorFactory.fromResource(icoCeltic))); 
     markers.add(purple); 
     orange = mMap.addMarker(new MarkerOptions().position(new LatLng(55.84505,-4.17607)).icon(BitmapDescriptorFactory.fromResource(icoTollcross))); 
     markers.add(orange); 
     cyan = mMap.addMarker(new MarkerOptions().position(new LatLng(55.82570,-4.25239)).icon(BitmapDescriptorFactory.fromResource(icoHampden))); 
     markers.add(cyan); 
     magenta = mMap.addMarker(new MarkerOptions().position(new LatLng(55.79550,-4.22329)).icon(BitmapDescriptorFactory.fromResource(icoCathkin))); 
     markers.add(magenta); 
     olive = mMap.addMarker(new MarkerOptions().position(new LatLng(55.78529,-4.01481)).icon(BitmapDescriptorFactory.fromResource(icoStrathclyde))); 
     markers.add(olive); 
     darkBlue = mMap.addMarker(new MarkerOptions().position(new LatLng(55.93920,-3.17273)).icon(BitmapDescriptorFactory.fromResource(icoEdinburgh))); 
     markers.add(darkBlue);   
     LightGreen = mMap.addMarker(new MarkerOptions().position(new LatLng(56.49302,-2.74663)).icon(BitmapDescriptorFactory.fromResource(icoDundee))); 
     markers.add(LightGreen); 
     //dundeeM = mMap.addMarker(new MarkerOptions().position(new LatLng(56.49302,-2.74663)).title("DUNDEE")); 
    } 
    markers.size(); } 


@Override 
public boolean onMarkerClick(Marker marker) 
{ 
    if(marker.equals(marker.getId())) 
    { 
     // Create custom dialog object 
     final Dialog dialog = new Dialog(MucMainActivity.this); 
     // Include dialog.xml file 
     dialog.setContentView(R.layout.custom); 
     // Set dialog title 
     dialog.setTitle(marker.getTitle()); 

     // set values for custom dialog components - text, image and button 
     TextView text = (TextView) dialog.findViewById(R.id.textDialog); 
     text.setText("Cathkin Braes Country Park" + "\n"+ "cathkin Road" + "\n" + "Glasgow" + "\n" + "G45"); 
     ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog); 
     image.setImageResource(R.drawable.biking); 

     dialog.show(); 

     Button declineButton = (Button) dialog.findViewById(R.id.declineButton); 
     // if decline button is clicked, close the custom dialog 
     declineButton.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       // Close dialog 
       dialog.dismiss(); 
      } 
     });   
    } 


    return true; 
} 

回答

1

裏面的方法

public boolean onMarkerClick(Marker marker) 

你有一個條件檢查:

if(marker.equals(marker.getId())) 

嘗試刪除它。您將標記存儲在開始定義的私有成員中

private List<Marker> markers = new ArrayList<Marker>(); 

所以我不明白爲什麼要檢查標記等於標記ID。

希望它有幫助。

0

確實是這個問題,刪除條件檢查解決了這個問題。由於少年

更新走的是如下

@Override 
public boolean onMarkerClick(Marker marker) 
{   

    { 
     // Create custom dialog object 
     final Dialog dialog = new Dialog(MucMainActivity.this); 
     // Include dialog.xml file 
     dialog.setContentView(R.layout.custom); 
     // Set dialog title 
     dialog.setTitle(marker.getTitle()); 

     // set values for custom dialog components - text, image and button 
     TextView text = (TextView) dialog.findViewById(R.id.textDialog); 
     text.setText(marker.getSnippet()); 
     ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog); 
     image.setImageResource(R.drawable.biking); 

     dialog.show(); 

     Button declineButton = (Button) dialog.findViewById(R.id.declineButton); 
     // if decline button is clicked, close the custom dialog 
     declineButton.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       // Close dialog 
       dialog.dismiss(); 
      } 
     });   
    }  

    return true; 
}