简体   繁体   English

单击标记时的警报对话框布局

[英]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.我搜索了如何在单击 google maps api v3 上的标记时显示警报对话框布局。 I found a code here on stackoverflow but for me it's not working.我在 stackoverflow 上找到了一个代码,但对我来说它不起作用。 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 块中的代码运行,该方法应该返回 true。 If the method returns false the click event will not be passed to the listener you have registered.如果该方法返回 false,则单击事件将不会传递给您已注册的侦听器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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