地图不向用户开放';Android中的位置
每当我运行我的Android代码,我的地图在我的真实设备上打开时(不是在模拟器上),它不会指向任何地方,它只是显示缩小的世界地图,而不是用户的位置。我尝试了各种各样的组合来实现这一点,但没有任何帮助 下面是我试图获取用户的行为的代码,但行为仍然保持不变。我尝试将GPS_服务改为网络_提供商,但仍然没有任何帮助。这里的解决方案可能是什么 我在这里测试的设备是1加3 Manifest.xml:地图不向用户开放';Android中的位置,android,google-maps-android-api-2,Android,Google Maps Android Api 2,每当我运行我的Android代码,我的地图在我的真实设备上打开时(不是在模拟器上),它不会指向任何地方,它只是显示缩小的世界地图,而不是用户的位置。我尝试了各种各样的组合来实现这一点,但没有任何帮助 下面是我试图获取用户的行为的代码,但行为仍然保持不变。我尝试将GPS_服务改为网络_提供商,但仍然没有任何帮助。这里的解决方案可能是什么 我在这里测试的设备是1加3 Manifest.xml: <uses-permission android:name="android.permission.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
地图活动性:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
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(requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, 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);
}
/**
* 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.setMapType(GoogleMap.MAP_TYPE_HYBRID);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Add a marker on user's location and move the camera.
LatLng yourLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(yourLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(yourLocation));
// To get the address of the user location.
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
// 1 - represents only one location is required.
List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if(listAddresses != null && listAddresses.size() > 0) {
Log.i("PlaceInfo", listAddresses.get(0).toString());
String address = "";
if(listAddresses.get(0).getSubThoroughfare() != null) {
address += listAddresses.get(0).getSubThoroughfare() + " ";
}
if(listAddresses.get(0).getThoroughfare() != null) {
address += listAddresses.get(0).getThoroughfare() + ", ";
}
if(listAddresses.get(0).getLocality() != null) {
address += listAddresses.get(0).getLocality() + ", ";
}
if(listAddresses.get(0).getPostalCode() != null) {
address += listAddresses.get(0).getPostalCode() + ", ";
}
if(listAddresses.get(0).getCountryName() != null) {
address += listAddresses.get(0).getCountryName();
}
Toast.makeText(MapsActivity.this, address, Toast.LENGTH_LONG).show();
Log.i("address", address);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@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) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_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.NETWORK_PROVIDER, 0, 0, locationListener);
// Get the users's current location.
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LatLng yourLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(yourLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(yourLocation));
}
}
}
}
公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{
私有谷歌地图;
地点经理地点经理;
LocationListener LocationListener;
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
if(requestCode==1){
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
if(ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u FINE\u位置)==PackageManager.permission\u已授予){
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,0,0,locationListener);
}
}
}
}
@凌驾
创建时受保护的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.setMapType(GoogleMap.MAP\u TYPE\u HYBRID);
locationManager=(locationManager)this.getSystemService(Context.LOCATION\u服务);
locationListener=新locationListener(){
@凌驾
已更改位置上的公共无效(位置){
//在用户位置上添加标记并移动相机。
LatLng yourLocation=新LatLng(location.getLatitude(),location.getLongitude());
mMap.clear();
mMap.addMarker(新的MarkerOptions().position(yourLocation).title(yourLocation));
mMap.moveCamera(CameraUpdateFactory.newLatLng(您的位置));
//获取用户位置的地址。
Geocoder Geocoder=新的Geocoder(getApplicationContext(),Locale.getDefault());
试一试{
//1-表示只需要一个位置。
List listAddresses=geocoder.getFromLocation(location.getLatitude(),location.getLatitude(),1);
if(listAddresses!=null&&listAddresses.size()>0){
Log.i(“PlaceInfo”,listAddresses.get(0.toString());
字符串地址=”;
if(listAddresses.get(0).GetSubThroughure()!=null){
address+=listAddresses.get(0).GetSubThroughure()+“”;
}
if(listAddresses.get(0.GetThroughure()!=null){
address+=listAddresses.get(0.GetThroughure()+“,”;
}
if(listAddresses.get(0.GetLocation()!=null){
address+=listAddresses.get(0).GetLocation()+“,”;
}
if(listAddresses.get(0.getPostalCode()!=null){
address+=listAddresses.get(0).getPostalCode()+“,”;
}
if(listAddresses.get(0).getCountryName()!=null){
address+=listAddresses.get(0.getCountryName();
}
Toast.makeText(MapsActivity.this,address,Toast.LENGTH_LONG.show();
Log.i(“地址”,地址);
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
状态已更改的公共void(字符串s、int i、Bundle){
}
@凌驾
已提供已启用的公共void(字符串s){
}
@凌驾
公共无效onProviderDisabled(字符串s){
}
};
如果(Build.VERSION.SDK_INT<23){
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,0,0,locationListener);
}
否则{
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予){
ActivityCompat.requestPermissions(这个新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},1);
}否则{
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,0,0,locationListener);
//获取用户的当前位置。
位置lastKnownLocation=locationManager.getLastKn