1

我想使用谷歌地图制作标记应用程序,但是 . 我对使用 switch Case 的 onMarkerClick 有问题,我使用数组将 Marker 添加到我的应用程序中,当标记 OnCLick 时,他可以为每个标记调用不同的 Activity。我对此有疑问..我如何将 onMarkerClick 与 switch case 一起用于我的应用程序。 .??? 请帮忙 。这是我的代码:

public static final String TAG = markerbanyak.TAG;
    final LatLng CENTER = new LatLng(43.661049, -79.400917);


    class Data {
        public Data(float lng, float lat, String title, String snippet, String icon) {
            super();
            this.lat = (float)lat;
            this.lng = (float)lng;
            this.title = title;
            this.snippet = snippet;
            this.icon = icon;
        }
        float lat;
        float lng;
        String title;
        String snippet;
        String icon;
    }

    Data[] data = {
            new Data(-79.400917f,43.661049f, "New New College Res",
                    "Residence building (new concrete high-rise)", "R.drawable.mr_kun"),
            new Data(-79.394524f,43.655796f, "Baldwin Street",
                    "Here be many good restaurants!", "R.drawable.mr_kun"),
            new Data(-79.402206f,43.657688f, "College St",
                    "Lots of discount computer stores if you forgot a cable or need to buy hardware.", "R.drawable.mr_kun"),    
            new Data(-79.390381f,43.659878f, "Queens Park Subway",
                    "Quickest way to the north-south (Yonge-University-Spadina) subway/metro line", "R.drawable.mr_kun"),
            new Data(-79.403732f,43.666801f, "Spadina Subway",
                    "Quickest way to the east-west (Bloor-Danforth) subway/metro line", "R.drawable.mr_kun"),
            new Data(-79.399696f,43.667873f, "St George Subway back door",
                    "Token-only admittance, else use Spadina or Bedford entrances!", "R.drawable.mr_kun"),
            new Data(-79.384163f,43.655083f, "Eaton Centre (megamall)",
                    "One of the largest indoor shopping centres in eastern Canada. Runs from Dundas to Queen.", "R.drawable.mr_kun"),
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.peta);

        SupportMapFragment supportMapFragment = (SupportMapFragment)
        getSupportFragmentManager().findFragmentById(R.id.map);

                // Getting a reference to the map
        mMap = supportMapFragment.getMap();

        Marker kuningan = mMap.addMarker(new MarkerOptions()
        .position(KUNINGAN)
        .title("Kuningan")
        .snippet("Kuningan ASRI")
        .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.mr_kun)));

        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(KUNINGAN, 2));

        // Zoom in, animating the camera.
        mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);



        }

     public void wisata(){

            if (mMap==null) {
                Log.d(TAG, "Map Fragment Not Found or no Map in it!!");
                return;
            }

            for (Data d : data) {
                LatLng location = new LatLng(d.lat, d.lng);
                Marker wisata =mMap.addMarker(new MarkerOptions()
                      .position(location)
                      .title(d.title)
                      .snippet(d.snippet)
                      .icon(BitmapDescriptorFactory.fromResource(R.drawable.mr_wis)));

                // Let the user see indoor maps where available.
                mMap.setIndoorEnabled(true);

                // Enable my-location stuff
                mMap.setMyLocationEnabled(true);

                // Move the "camera" (view position) to our center point.
                mMap.moveCamera(CameraUpdateFactory.newLatLng(CENTER));
                        // Then animate the markers while the map is drawing,
                // since you can't combine motion and zoom setting!
                        final int zoom = 14;
                        mMap.animateCamera(CameraUpdateFactory.zoomTo(zoom), 2000, null);

                mMap.setOnMarkerClickListener(new OnMarkerClickListener() {

                    @Override
                    public boolean onMarkerClick(Marker v) {
                        // TODO Auto-generated method stub
                        //(PLEASE HELP ME !!! :))

                        return false;
                    }
                });
            }

            }
4

2 回答 2

2
                @Override
                public boolean onMarkerClick(Marker v) {
                    // TODO Auto-generated method stub
                    //(PLEASE HELP ME !!! :))
                 if(v.getTitle().contains("New New College Res")){
                    // do if marker has this title
                    }else if(v.getTitle().contains("Baldwin Street")){
                    // do if marker has this title
                    } // and so on
                    return false;
                }
            });
于 2013-04-25T02:33:40.827 回答
0

标记类如果是最终的,因此您不能扩展它并添加自己的属性。您将不得不使用自己的逻辑来处理这个问题。

您可以通过两种方式做到这一点。

您已经有一个包含所有标记数据的数据列表。

1)您可以尝试使用 Data 数组的数组索引设置 Marker 的“snippet”属性,在 onMarkerClick 上,您可以让 Snippet 将其更改回 int,这将是您的 Data Array 的索引。所以你可以得到你点击的 Marker 和它的 Data 对象,然后做任何你想做的事情。

2)使用哈希映射。

它看起来像这样

HashMap<Marker, Integer> hashMap = new HashMap<Marker, Integer>();

// now even in your loop where you are adding markers. you can also add that marker to this hashmap with the id of Data array's index.

hashMap.add(marker, indexOfDataArray);

// final in your onMarkerClick, you can just pass marker to hashMap and get indexOfDataArray and base of that do whatever you want to do.

int id = hashMap.get(marker);
于 2013-04-25T02:53:21.127 回答