Jamaludin Iqba
Jamaludin Iqba

Reputation: 191

onMarkerClick using switch Case

I want to make marker Application using Google Maps but . I Have problem about onMarkerClick using switch Case, Iam using array to add Marker to My Application and when marker OnCLick he can call different Activity for each marker .I Have problom about that.. How I can using onMarkerClick with switch case for my application..??? Please Help . Here is My Code :

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;
                    }
                });
            }

            }

Upvotes: 1

Views: 943

Answers (2)

Sharjeel
Sharjeel

Reputation: 15798

Marker class if final so you can't extend it and add your own attributes. You will have to handle this using your own logic.

You can do this in two ways.

You already have a Data list with all marker data.

1) You could try to set "snippet" attribute of Marker with your array index of Data array and on onMarkerClick you can get Snippet change it back to int and that would be your Data Array's index. So you can get that your clicked Marker and it's Data object and do whatever you want to do.

2) Use HashMap.

It will look something like this

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);

Upvotes: 0

JRowan
JRowan

Reputation: 7114

                @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;
                }
            });

Upvotes: 2

Related Questions