尝试将我的地图居中放在用户位置(表示位置为空) [英] Trying to center my map on user location (Says location is null)

查看:183
本文介绍了尝试将我的地图居中放在用户位置(表示位置为空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直试图在用户位置上居中放置一张地图,但它一直说latLng变量为空。我读过那种情况,因为我使用getLastKnownLocation,但我无法找到任何其他方式来做到这一点(我是编程新手)。我正在使用Jellybean上的google api。这是我正在使用的代码。

  if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED){
mMap.setMyLocationEnabled(true);

LocationManager locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
标准标准=新标准();
String provider = locationManager.getBestProvider(criteria,true);
位置myLocation = locationManager.getLastKnownLocation(provider);

//纬度y纵向
双纬度= myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latLng =新LatLng(经度,纬度);
// Mover el mapa a la posicion actual del usuario
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//放大
mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
mMap.addMarker(new MarkerOptions()。position(new LatLng(latitude,longitude))。title(You are here!)。snippet(考虑自己定位));






}其他{
//显示原理和请求权限。


解决方案

检查AndroidManifest。 xml

 < user-permission android:name =android.permission.ACCESS_FINE_LOCATION /> 
< user-permission android:name =android.permission.ACCESS_COARSE_LOCATION />
< user-permission android:name =android.permission.INTERNET />

主要我们可以通过GPS提供商和网络提供商获取位置信息。
如果我们通过GPS提供商获得空位置,则将提供商更改为NETWORK。


$ b

  LocationManager locationManager =(LocationManager)getSystemService (Context.LOCATION_SERVICE); 
字符串provideGps = locationManager.GPS_PROVIDER;
字符串provideNetwork = locationManager.NETWORK_PROVIDER;

位置位置= locationManager.getLastKnownLocation(providerGps);

如果(location == null){
location = locationManager.getLastKnownLocation(providerNetwork);
location.getLatitude();
location.getLongitude();
} else {
location.getLatitude();
location.getLongitude();
}


ive been trying to center a map on the users location, but it keeps saying the latLng variable is null. I have read that that happens because i am using the getLastKnownLocation, but I cant find any other way to do it (I am new to programing). I am working with the google api on Jellybean. Here is the code that i am using.

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);

 LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria,true);
        Location myLocation = locationManager.getLastKnownLocation(provider);

        //Latitude y longitud
        double latitude = myLocation.getLatitude();
        double longitude = myLocation.getLongitude();
        LatLng latLng = new LatLng(latitude,longitude);
        //Mover el mapa a la posicion actual del usuario
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        //Zoom
        mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
        mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));






      } else {
            // Show rationale and request permission.
        }

解决方案

Check permission in "AndroidManifest.xml

<user-permission android:name="android.permission.ACCESS_FINE_LOCATION />
<user-permission android:name="android.permission.ACCESS_COARSE_LOCATION />
<user-permission android:name="android.permission.INTERNET />

Mainly We can get location via GPS Provider and NETWORK provider. If we get null location Via GPS provider, then change Provider to NETWORK.

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provideGps = locationManager.GPS_PROVIDER;
String provideNetwork = locationManager.NETWORK_PROVIDER;

Location location = locationManager.getLastKnownLocation(providerGps);

If (location == null) {
    location = locationManager.getLastKnownLocation(providerNetwork);
    location.getLatitude();
    location.getLongitude();
} else {
    location.getLatitude();
    location.getLongitude();
}

这篇关于尝试将我的地图居中放在用户位置(表示位置为空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆