javascript - 优化安卓代码

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

在我的应用程序中,我在 map 上绘制了 12 个标记,然后对于这些标记中的每一个,我都有 12 个对话框弹出并显示标记位置的地址。我在想一定有更好的方法来做到这一点,所以我一直在尝试创建一个列表来保存所有标记,并在点击时将列表中的标记传递到对话框中,所以我不必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;
}   

最佳答案

方法内部

public boolean onMarkerClick(Marker marker)

你有条件检查:

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

尝试删除它。您将标记存储在开头定义的私有(private)成员中

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

所以我不明白为什么要检查标记是否等于标记 ID。

希望对您有所帮助。

关于javascript - 优化安卓代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20623790/

相关文章:

javascript - 返回一个由 : one first item, 两个第二项、树第三项等组成的数组,不使用循环

android - 当应用程序处于后台时, Activity 和 fragment 会发生什么

google-maps - 新的谷歌地图嵌入选项

Javascript 为什么 console.log(typeof "Not a Number"- "Number") 打印 'NaN' ?

javascript - 我如何从 formData 中获取内容?

java - 如何让ListView调整大小?

android - 查找父级 id 为 X 的所有子 Realm 对象

php - Google map 商店定位器 PHP/MySQL

javascript - map 边界更改时显示新标记并清除旧标记

javascript - 使用 pagemod 添加一些 js 到 contentscript