片段中可点击的Google Maps标记 [英] Clickable Google Maps marker in Fragment
问题描述
当我单击加载时动态添加的标记(调用API,获取位置并在地图上显示它们)时,我要在此处完成的任务是能够显示AlertDialog.这是在片段onCreateView
中完成的.
What I am trying to accomplish here is be able to show a AlertDialog when I click on a marker that is dynamically added on load (call an API, get positions and show them on map). This is done in the fragments onCreateView
.
地图在这里加载:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_container, mapFragment).commit();
}
}
稍后,我从SupportMapFragment
切换到GoogleMap
:
@Override
public void onResume() {
super.onResume();
if (mMap == null && mapFragment != null) {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = Tools.configBasicGoogleMap(googleMap);
mMap.setMapType(sharedPref.getMapType());
}
});
}
}
因此,从逻辑上讲,我的地图应该已经准备好为mMap
.现在,我想显示一个AlertDialog,所以在片段的类定义中执行implements GoogleMap.OnMarkerClickListener
并在此处实现它:
So, logically, my Map should be ready as mMap
. Now, I want to show a AlertDialog, so I do implements GoogleMap.OnMarkerClickListener
in the class definition for the fragment and implement it here:
@Override
public boolean onMarkerClick(final Marker marker) {
Toast.makeText(getContext(), "Hello world", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Confirmation");
builder.setMessage("Pour confirmer le rapport, appuyez sur + 1.\nSi le rapport est faux, cliquez sur - 1.");
builder.setPositiveButton("+ 1", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
URL link = null;
try {
link = new URL(S.base_url + "report/increment/" + marker.getTag().toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(
link.openStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
});
AlertDialog ab = builder.create();
ab.show();
return false;
}
但是,甚至连Toast都没有显示出来……当我单击一个标记时,会显示方向"和在Google Maps中打开"按钮,并且地图以标记为中心,但是我的方法没有开始.
But not even the Toast doesn't show up... When I click on a marker I get the Directions and Open in Google Maps buttons, and the map centers itself on the marker, but my method doesn't start.
总而言之,从逻辑上来说,当我单击加载的这些标记之一时,onMarkerClick方法应该触发自身,但是这种情况永远不会发生!知道为什么吗?谢谢.
To conclude, logically when I click one of these markers that I load, the method onMarkerClick should trigger itself, but this never happens! Any idea why? Thanks.
推荐答案
我不知道为什么,但是 Google Maps API 不能用作片段的实现.以下代码可能对您有用.您只需要为本地分配markerClickListener即可,而不是实现MarkerClickListener.
I don't know why but Google Maps API can't used as implement to fragments. Following code may work it for you. You just need to assign markerClickListener for local, not implementing MarkerClickListener.
//Detect location and set on map
mMapView.getMapAsync(new OnMapReadyCallback() {
@SuppressLint("MissingPermission")
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
// Bla
// Bla
// Bla your codes about map
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// Triggered when user click any marker on the map
return false;
}
});
}
});
这篇关于片段中可点击的Google Maps标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!