Android Google Map - Clicked marker opens new activity or bigger window

前端 未结 5 1624
梦毁少年i
梦毁少年i 2020-12-14 02:41

I\'ve been searching for help on implementing OnMarkerClickListener but nothing I\'ve found has worked. This is my marker below and when clicked it only changes colour(light

相关标签:
5条回答
  • 2020-12-14 02:51

    I suggest use of OnInfoWindowClickListener, it will trigger when you click on marker and then the snippet. Use setTag to attach any object with the marker.

        Marker marker = mMap.addMarker(markerOptions);
        marker.setTag(myObject);
    

    and the listener

          mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
    
            @Override
            public void onInfoWindowClick(Marker arg0) {
    
             MyObject mo =  (MyObject )arg0.getTag();
    
    
               }
           });
    
    0 讨论(0)
  • 2020-12-14 02:53

    Below code to used for Kotlin when user click on marker to perform any action

            googleMap!!.setOnMarkerClickListener { marker ->
                if (marker.title == "Marker1")
                    Log.d(TAG, "Clicked on Marker1")
                true
            }
    
    0 讨论(0)
  • 2020-12-14 02:55

    If you need the event Click in a market,this code it's the solution.

    private GoogleMap mGoogleMap;
    
    
    mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
            {
    
                @Override
                public boolean onMarkerClick(Marker arg0) {
                    if(arg0 != null && arg0.getTitle().equals(markerOptions2.getTitle().toString())); // if marker  source is clicked
                        Toast.makeText(menu.this, arg0.getTitle(), Toast.LENGTH_SHORT).show();// display toast
                    return true;
                }
    
            });
    

    Good Luck

    0 讨论(0)
  • 2020-12-14 02:56

    Marker click events

    Don't snap to marker after click in android map v2

    Quoting from the above post

    You can use an OnMarkerClickListener to listen for click events on the marker. To set this listener on the map, call GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user clicks on a marker, onMarkerClick(Marker) will be called and the marker will be passed through as an argument. This method returns a boolean that indicates whether you have consumed the event (i.e., you want to suppress the default behavior). If it returns false, then the default behavior will occur in addition to your custom behavior. The default behavior for a marker click event is to show its info window (if available) and move the camera such that the marker is centered on the map.

    https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMarkerClickListener.

    Use OnMarkerClickListener on your marker.

    Check the link for code snippets

    Google Maps API v2: How to make markers clickable?

    Example: Works on my phone

    
        Marker source, destination;
        GoogleMap mMap;
    
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        source = mMap.addMarker(new MarkerOptions()
                .position(sc)
                .title("MyHome")
                .snippet("Bangalore")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
    
        destination = mMap.addMarker(new MarkerOptions()
                .position(lng)
                .title("MapleBear Head Office")
                .snippet("Jayanager")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
    
        mMap.setOnMarkerClickListener(marker -> {
            if (marker.getTitle().equals("MyHome")) // if marker source is clicked
                Toast.makeText(MainActivity.this, marker.getTitle(), Toast.LENGTH_SHORT).show();// display toast
            return true;
        });
    
    
    0 讨论(0)
  • 2020-12-14 02:59

    This code handles the maker click event and loads a new layout (XML) with some information:

    /**
     * adding individual markers, displaying text on on marker click on a
     * bubble, action of on marker bubble click
     */
    private final void addLocationsToMap() {
        int i = 0;
        for (Stores store : storeList) {
            LatLng l = new LatLng(store.getLatitude(), store.getLongtitude());
    
            MarkerOptions marker = new MarkerOptions()
                    .position(l)
                    .title(store.getStoreName())
                    .snippet("" + i)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            googleMap.addMarker(marker);
            ++i;
        }
    
        googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
    
            @Override
            public void onInfoWindowClick(Marker marker) {
    
                try {
                    popUpWindow.setVisibility(View.VISIBLE);
                    Stores store = storeList.get(Integer.parseInt(marker
                            .getSnippet()));
    
                    // set details
                    email.setText(store.getEmail());
                    phoneNo.setText(store.getPhone());
                    address.setText(store.getAddress());
    
                    // setting test value to phone number
                    tempString = store.getPhone();
                    SpannableString spanString = new SpannableString(tempString);
                    spanString.setSpan(new UnderlineSpan(), 0,
                            spanString.length(), 0);
                    phoneNo.setText(spanString);
    
                    // setting test value to email
                    tempStringemail = store.getEmail();
    
                    SpannableString spanString1 = new SpannableString(tempStringemail);
                    spanString1.setSpan(new UnderlineSpan(), 0, spanString1.length(), 0);
                    email.setText(spanString1);
    
                    storeLat = store.getLatitude();
                    storelng = store.getLongtitude();
    
                } catch (ArrayIndexOutOfBoundsException e) {
                    Log.e("ArrayIndexOutOfBoundsException", " Occured");
                }
    
            }
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题