java - G Maps Android Java,最后一个已知的位置标记颜色与位置标记不同,但在运行的应用程序上显示相同的标记颜色
问题描述
//关注代码的最后一个 else 块和 OnLocationChanged 方法(在 OnMapReady() 方法内部)
这个 else 块(包含 lastknown location 方法的代码)和 onLocationChanged 方法
包含正在更改的位置的代码(此处通过从模拟器可用的扩展控件传递,即在单击模拟器中最下方的选项时打开该框。)
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
LocationManager locationManager;
LocationListener locationListener;
private GoogleMap mMap;
@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 onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationListener=new LocationListener() {
@Override
public void onLocationChanged(Location location) {
mMap.clear();
LatLng place = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(place).title("Marker in Sydney").icon(BitmapDescriptorFactory.defaultMarker(HUE_YELLOW)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
mMap.animateCamera(CameraUpdateFactory.zoomTo(3));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
// Add a marker in Sydney and move the camera
if(Build.VERSION.SDK_INT<23)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}else
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}
else
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
Location lastknownlocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng position=new LatLng(lastknownlocation.getLatitude(),lastknownlocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(position).title("The Location is"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
}
}
解决方案
就像在第一个标记中一样,将黄色图标添加到第二个标记,如下所示:
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastknownlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng position = new LatLng(lastknownlocation.getLatitude(), lastknownlocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(position).title("The Location is").icon(BitmapDescriptorFactory.defaultMarker(HUE_YELLOW)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
截屏:
希望这可以帮助!
推荐阅读
- java - 即使声明了 Hibernate 的 FetchType.LAZY,也会获取实体集合
- java - Spring boot elasticsearch @Field名称不起作用
- css - 收到错误消息时如何从 css 添加背景
- python-3.x - 我的想法正确吗?Python 条件键盘记录器
- python - 使用 tcp 套接字时出现输入问题
- recharts - 在 ReChart 中动态添加系列?
- python - 使用旋转 python 进行高斯消除
- apache-spark - 通过跳过 PySpark 中的最后一行来创建 RDD
- swiftui - swiftui - 如何从弹出模式视图中引用父视图?(附代码)
- python - Redis Streams 使用 XREAD 获取多个值