CODE
public class BlueFragment2 extends Fragment implements OnMapReadyCallback, GoogleMap.OnMyLocationButtonClickListener {
private GoogleMap googleMap;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blue, container, false);
MapView mapview = (MapView) view.findViewById(R.id.map_blue);
mapview.onCreate(savedInstanceState);
mapview.onResume();
mapview.getMapAsync(this);
return view;
}
@Override
public void onMapReady(final GoogleMap googleMap) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
}
@Override
public boolean onMyLocationButtonClick() {
LatLng point = new LatLng(googleMap.getMyLocation().getLatitude(), googleMap.getMyLocation().getLongitude());
MarkerOptions marker = new MarkerOptions()
.position(point).title("ok");
googleMap.clear();
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(point, 14));
googleMap.addMarker(marker);
return true;
}
}
我希望OnMyLocationButton点击将在谷歌地图上添加标记。
但它没有用。
如果我在public void onMapReady
上写了
@Override
public void onMapReady(final GoogleMap googleMap) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
googleMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
LatLng point = new LatLng(googleMap.getMyLocation().getLatitude(), googleMap.getMyLocation().getLongitude());
MarkerOptions marker = new MarkerOptions()
.position(point).title("ok");
googleMap.clear();
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(point, 14));
googleMap.addMarker(marker);
return true;
}
});
}
效果很好。
为什么这不起作用?
你会帮我吗?问题:
除了在public void onMapReady
上书写之外还有其他方法吗?
使用implements googleMap.OnMyLocationButtonClickListener
是否有问题?