[英]Android Studio Google Maps Emulator stuck on location?
我是 Java 的新学习者。 正如标题所描述的,我无法在模拟器 PIXEL 2 API 29 中的谷歌地图上自由漫游。相机卡在位置的中心,无论如何我都无法将相机带到其他地方。 我不认为这是 API,因为我在没有代码的情况下对其进行了测试。 开放给建议,谢谢。 这是代码;
`
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
@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(GoogleMap googleMap) {
mMap = googleMap;
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,15));
System.out.println("location: " + location);
}
};
if(Build.VERSION.SDK_INT >= 23) {
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,locationListener);
}
}else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,locationListener);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length> 0){
if(requestCode==1){
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,locationListener);
}
}
}
}
}`
这是因为代码中LocationLister
onLocationChanged()
函数将相机移动到设备的当前位置,只要地图的中心不在设备的当前位置,就会触发该位置。
public void onLocationChanged(Location location) {
LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,15));
System.out.println("location: " + location);
}
LocationListener
位置是使用locationManager.requestLocationUpdates()
通过位置管理器服务注册的。 要了解更多信息,请参阅此处的文档。
如果您的主要目标是获取当前设备位置并在加载时将其显示在地图上,那么 Google Maps Platform 提供了一份官方文档来指导您如何操作。 要开始使用,您可以在此处查看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.