java - On Map Long Click listener 没有在我的 G Map 上放置标记,你能帮我吗?
问题描述
当我长按时帮助没有任何反应。另外,我在启动模拟器时收到这些消息,这可能与我的问题有关吗?其他一切似乎都在应用程序中工作。
这是我的代码:
公共类 MapsActivity 扩展 FragmentActivity 实现 OnMapReadyCallback、GoogleMap.OnMapLongClickListener {
@Override
public void onMapLongClick(LatLng latLng) {
Log.i("Map", "CLICKED");
mMap.addMarker(new MarkerOptions().position(latLng).title("Your new Memorable Place").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
Toast.makeText(getApplicationContext(),
"New marker added@" + latLng.toString(), Toast.LENGTH_LONG)
.show();
}
LocationManager locationManager;
LocationListener locationListener;
private GoogleMap mMap;
public void centerMapOnLocation (Location location, String title) {
if (location != null) {
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 12));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
centerMapOnLocation(lastKnownLocation, "Your Location");
}
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Intent intent = getIntent();
if (intent.getIntExtra("placeNumber", 0) == 0){
//Zoom in on user location
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
centerMapOnLocation(location, "Your Location");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
centerMapOnLocation(lastKnownLocation, "Your Location");
}else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
}
}
解决方案
我认为您错过了将侦听器添加到地图中。
@Override
public void onMapReady(GoogleMap googleMap){
mMap=googleMap;
mMap.setOnMapLongClickListener(this);
//rest of your code.
}
推荐阅读
- r - R中的页面功能
- javascript - 为什么我不断收到 $(...).scrollSpy 未定义?
- flutter - 如何使用 Flutter 实现类似 ios 虚拟主页按钮的按钮
- qt5 - VLC 构建系统找不到 Qt5
- encoding - 如何使用 LispWorks 正确检测文件编码?
- nlp - 使用 word2vec 聚类相似词
- mongodb - 使用条件构建 MongoDB 查询
- javascript - 从 Web 浏览器启动本地可执行文件
- html - -webkit-overflow-scrolling:触摸和模态对话框(z-index 问题)
- html - 如何定位与导航栏相关的侧面部分或 div?