简体   繁体   中英

Alert dialog layout when clicking marker

I searched on how to display an alert dialog layout when clicking a marker on google maps api v3. I found a code here on stackoverflow but for me it's not working. Here is my map class

package com.myapp;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.graphics.Color;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;


public class MyClass extends FragmentActivity implements OnMarkerClickListener {
    private GoogleMap googleMap;
    private int mapType = GoogleMap.MAP_TYPE_NORMAL;
    private Marker marker1;
    ImageButton imageButton1;
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        imageButton1 = (ImageButton) findViewById(R.id.imageButton1); 

        FragmentManager fragmentManager = getFragmentManager();
        MapFragment mapFragment =  (MapFragment) fragmentManager.findFragmentById(R.id.map);
        googleMap = mapFragment.getMap();

     // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);
     // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
       googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                LatLng sLatLng12 = new LatLng(34.0522, 118.2437);
                marker1 = googleMap.addMarker(new MarkerOptions()
                .position(sLatLng12)
                .title("Marker title")
                .snippet("Marker snippet")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));





    public boolean onMarkerClick(Marker marker1) {
        if(this.marker1.equals(marker1)){
        AlertDialog.Builder alertadd = new AlertDialog.Builder(MyClass.this);
        LayoutInflater factory = LayoutInflater.from(MyClass.this);
        final View view = factory.inflate(R.layout.dialog_layout, null);
        alertadd.setView(view);
        alertadd.show();
      }
      return false;
    }


}

The map opens fine and showing my marker. But when I click on, it doesn't show any alert dialog. Thanks in advance!

The method should return true in case the code inside if block runs. If the method returns false the click event will not be passed to the listener you have registered.

public boolean onMarkerClick(Marker marker1) {
        if(this.marker1.equals(marker1)){
        AlertDialog.Builder alertadd = new AlertDialog.Builder(MyClass.this);
        LayoutInflater factory = LayoutInflater.from(MyClass.this);
        final View view = factory.inflate(R.layout.dialog_layout, null);
        alertadd.setView(view);
        alertadd.show();
        return true;
      }
      return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM