我搜索了在google maps 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;
}
}
地图打开正常并显示我的标记。但是当我点击它时,它不会显示任何警告对话框。 提前谢谢!
答案 0 :(得分:1)
如果块中的代码运行,则该方法应该返回true。如果方法返回false,则click事件将不会传递给您已注册的侦听器。
break