我搜索了如何在谷歌地图api v3上单击标记时显示警报对话框布局。我在stackoverflow上找到了一个代码,但对我来说它不起作用。这是我的地图类
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;
}
}
地图打开得很好,显示了我的记号笔。但当我点击时,它不会显示任何警报对话框。提前感谢!
如果块内部的代码运行,则该方法应返回true。如果该方法返回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;
}