我在Android应用中使用Google地图。我需要将地图重新定位到客户端的当前位置。我使用了以下声明 -
map.setmylocationenabled(true);
这会在右上方显示一个按钮,但点击该按钮不起作用。
按钮单击侦听器:
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
mMap.addMarker(new MarkerOptions().position(myLatLng).title("My Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLatLng, zoomLevel));
return false;
}
});
答案 0 :(得分:7)
最后一行是我的解决方案:
myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);
答案 1 :(得分:4)
只需从my other answer here获取代码,然后修改按钮点击监听器以请求其他位置:
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
return false;
}
});
onLocationChanged()中的代码然后将重新定位摄像机位置,然后再次取消注册位置更新:
@Override
public void onLocationChanged(Location location)
{
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
//move map camera
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
答案 2 :(得分:3)
使用后,您是否尝试获取经度和纬度?
setmylocationenabled(true)
?
例如
gMap.setMyLocationEnabled(true);
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
你现在可以使用纬度和经度,并将相机设置为你得到的纬度/经度位置。希望它有所帮助。
答案 3 :(得分:2)
您应该添加setMyLocationEnabled (boolean enabled)
。
启用或禁用“我的位置”图层。
在启用并且位置可用时,我的位置层 持续绘制用户当前位置的指示, 方位,并显示允许用户与之交互的UI控件 它们的位置(例如,启用或禁用相机跟踪 它们的位置和方位)。
要使用my-location-layer功能,您需要请求 ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION的权限 除非您设置了自定义位置来源。
演示
您应该在onMapReady (GoogleMap googleMap)
部分中添加它。
@Override
public void onMapReady(GoogleMap googleMap) {
googleMapOBJ = googleMap;
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#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 ActivityCompat#requestPermissions for more details.
return;
}
googleMapOBJ.setMyLocationEnabled(true);
googleMapOBJ.getUiSettings().setMyLocationButtonEnabled(true);
答案 4 :(得分:0)
科特琳的方式
mMap = googleMap
mMap.isMyLocationEnabled = true
mMap.uiSettings.isMyLocationButtonEnabled = true
答案 5 :(得分:0)
科特琳-
mMap!!.isMyLocationEnabled = true
mMap!!.uiSettings.isMyLocationButtonEnabled = true
Java-
myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);
答案 6 :(得分:0)
位置数据图层在地图的右上角添加了一个我的位置按钮。当用户点击按钮时,地图会以设备位置为中心。如果设备静止,则该位置显示为蓝点,如果设备正在移动,则显示为蓝色 V 形。
map.setMyLocationEnabled(Boolean) 调用需要权限,代码应明确检查权限是否可用。
或者,您可以使用注释取消丢失的位置权限。
您可以使用新的 Activity Result API
轻松实现这一目标这是代码
class MapsActivity : AppCompatActivity() , OnMapReadyCallback {
private lateinit var map: GoogleMap
//suppress missing permission
@SuppressLint("MissingPermission")
private val locationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) {
isGranted ->
if (isGranted) {
//add my Location Button on top-right side corner
map.isMyLocationEnabled = true
}
else {
ActivityCompat.requestPermissions(this ,
arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION) ,
0)
}
}
ActivityResultCallback 定义了应用如何处理用户对权限请求的响应。
要显示系统权限对话框,请调用 ActivityResultLauncher 实例上的 launch() 方法。
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
//call launch() passing in the permission required
locationPermission.launch(Manifest.permission.ACCESS_FINE_LOCATION)}
调用launch()后,系统权限对话框出现。当用户做出选择时,系统异步调用 ActivityResultCallback 的实现。
答案 7 :(得分:0)
我在我的真实设备中遇到了同样的问题,即使我是 add permission
位置。尽管如此,它仍然无法正常工作,我只是手动打开设备中的位置它的工作正常。