优化Android代码

时间:2013-12-17 00:37:06

标签: javascript android google-maps google-maps-api-3

在我的应用程序中,我正在向地图绘制12个标记,然后对于每个标记,我有12个dilalog框,弹出并显示标记位置的地址。我认为必须有一个更好的方法来做到这一点,所以我一直在尝试创建一个列表来保存所有标记,并在点击时将标记从列表传递到对话框中,所以我不必拥有12个高级对话框。

此刻我的标记被绘制到屏幕上,但是当标记被粘贴时对话框没有显示,但是当我使用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;
}   

2 个答案:

答案 0 :(得分:1)

方法内部

public boolean onMarkerClick(Marker marker)

你有条件检查:

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

尝试删除它。您将标记存储在开头

中定义的私有成员中
private List<Marker> markers = new ArrayList<Marker>();

所以我不明白你为什么要检查一个标记是否等于标记id。

希望它有所帮助。

答案 1 :(得分: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;
}