Java 标记消失,标题不显示
我试图通过长按位置并显示位置的名称在我的谷歌地图上添加一个标记,但是,每当我长按时,标记会出现一瞬间然后消失 我知道在长时间按下后,标记应该停留,然后显示标题,,但这不起作用 请让我知道,如果有人面临任何这样的问题,可以帮助我与它Java 标记消失,标题不显示,java,android,android-studio,google-maps,google-maps-markers,Java,Android,Android Studio,Google Maps,Google Maps Markers,我试图通过长按位置并显示位置的名称在我的谷歌地图上添加一个标记,但是,每当我长按时,标记会出现一瞬间然后消失 我知道在长时间按下后,标记应该停留,然后显示标题,,但这不起作用 请让我知道,如果有人面临任何这样的问题,可以帮助我与它 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener { private GoogleMa
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener {
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
@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, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
centerMapOnLocation(lastKnownLocation, "Your Location");
}
}
}
public void centerMapOnLocation(Location location, String title) {
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
if (title != "Your Location"){
mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 10));
}
@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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapLongClickListener(this);
Intent intent = getIntent();
if (intent.getIntExtra("placeNumber", 0) == 0) {
//zoom in on the user's 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 s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
if (Build.VERSION.SDK_INT < 23) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 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 onMapLongClick(LatLng latLng) {
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
String address ="";
try {
List <Address> listAddresses= geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);
if (listAddresses!=null&& listAddresses.size()>0){
if (listAddresses.get(0).getThoroughfare()!=null){
if (listAddresses.get(0).getSubThoroughfare()!=null){
address+=listAddresses.get(0).getSubThoroughfare()+" ";
}
address+= listAddresses.get(0).getThoroughfare();
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (address == ""){
SimpleDateFormat sdf = new SimpleDateFormat("mm:HH yyyyMMdd", Locale.getDefault());
address = sdf.format(new Date());
}
mMap.addMarker(new MarkerOptions().position(latLng).title(address).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
公共类MapsActivity扩展了FragmentActivity在Google Map.OnMapLongClickListener上实现的FragmentActivity{
私有谷歌地图;
地点经理地点经理;
LocationListener LocationListener;
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
如果(grantResults.length>0&&grantResults[0]==已授予PackageManager.PERMISSION){
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,locationListener);
Location lastKnownLocation=locationManager.getLastKnownLocation(locationManager.GPS\U提供程序);
centerMapOnLocation(lastKnownLocation,“您的位置”);
}
}
}
public void centerMapOnLocation(位置、字符串标题){
LatLng userLocation=新LatLng(location.getLatitude(),location.getLongitude());
mMap.clear();
如果(标题!=“您的位置”){
mMap.addMarker(新的MarkerOptions().position(userLocation).title(title));
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,10));
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
/**
*一旦可用,就可以操纵贴图。
*当映射准备好使用时,将触发此回调。
*这是我们可以添加标记或线条、添加侦听器或移动摄影机的地方。在这种情况下,
*我们只是在澳大利亚悉尼附近加了一个标记。
*如果设备上未安装Google Play服务,系统将提示用户安装
*它位于SupportMapFragment内。此方法仅在用户
*已安装Google Play服务并返回应用程序。
*/
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
mMap.setOnMapLongClickListener(这个);
Intent=getIntent();
if(intent.getIntExtra(“placeNumber”,0)==0){
//放大用户的位置
locationManager=(locationManager)this.getSystemService(Context.LOCATION\u服务);
locationListener=新locationListener(){
@凌驾
已更改位置上的公共无效(位置){
centerMapOnLocation(位置,“您的位置”);
}
@凌驾
状态已更改的公共void(字符串s、int i、Bundle){
}
@凌驾
已提供已启用的公共void(字符串s){
}
@凌驾
公共无效onProviderDisabled(字符串s){
}
};
如果(Build.VERSION.SDK_INT<23){
if(checkSelfPermission(Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和checkSelfPermission(Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//活动#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于活动#请请求权限以获取更多详细信息。
返回;
}
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,locationListener);
}否则{
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,locationListener);
Location lastKnownLocation=locationManager.getLastKnownLocation(locationManager.GPS\U提供程序);
centerMapOnLocation(lastKnownLocation,“您的位置”);
}否则{
ActivityCompat.requestPermissions(这个新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},1);
}
}
}
}
@凌驾
在MaplongClick(LatLng LatLng)上的公共无效{
Geocoder Geocoder=新的Geocoder(getApplicationContext(),Locale.getDefault());
字符串地址=”;
试一试{
List listAddresses=geocoder.getFromLocation(纬度,经度,1);
if(listAddresses!=null&&listAddresses.size()>0){
if(listAddresses.get(0.GetThroughure()!=null){
if(listAddresses.get(0).GetSubThroughure()!=null){
address+=listAddresses.get(0).GetSubThroughure()+“”;
}
address+=listAddresses.get(0.GetThroughure();
}
}
}捕获(IOE异常){
e、 printStackTrace();
}
如果(地址==“”){
SimpleDataFormat sdf=新的SimpleDataFormat(“mm:HH yyyyMMdd”,Locale.getDefault());
地址=sdf.format(新日期());
}
@Override
public void onLocationChanged(Location location) {
centerMapOnLocation(location, "Your Location");
}
mMap.clear();