在基于地图的应用程序中获取当前位置

时间:2018-12-18 17:28:36

标签: android android-maps-v2

我正在尝试在基于地图的应用程序中找到用户的当前位置,但是在同步数据时遇到了问题。代码在下面,但是基本上,我正在尝试运行一个函数,该函数(a)检查权限是否正确以及是否正确(b)提供当前的GPS或网络位置,然后(c)侦听位置改变。

代码的关键是我的Location mLocation属性;如果为null,则显示以默认LatLong为中心的地图,否则显示地图上的当前位置。

它的作用是:当我第一次运行它时,它会请求权限,但随后会显示默认的LatLong作为目标。如果我关闭应用程序并重新运行,则该应用程序将以当前用户LatLong为中心按预期运行。

我不知道为什么第一次尝试时不显示当前用户LatLong。我已经设置了断点,并且代码按预期运行,并显示了当前位置。但是,如果您在发布模式下运行它,或者没有任何断点,则初始视图为默认视图。

此链接(How to delay onMapready call in Google map based app)建议在getMapAsync之前在OnCreate中进行类似的调用以获取坐标,但这不起作用。

我的代码在下面,如果有人可以帮助!

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    setContentView(R.layout.activity_excursions);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    getCurrentLocation();

    mapFragment.getMapAsync(this);
}

private void getCurrentLocation()
{

    boolean bFineOK = false;
    boolean bCoarseOK = false;

    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        bFineOK = true;

    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        bCoarseOK = true;


    if (!bFineOK || !bCoarseOK )
    {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_ALL);
        //return;
    }
    else
    {
        // Everything OK, proceed as normal
    }


    boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


    if (!(isGPSEnabled || isNetworkEnabled))
    {

    }
    else {
        if (isNetworkEnabled)
        {
            if( bCoarseOK && bFineOK )
            {
                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                        LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
                mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }
            else
            {
                bTrack = false;
                bInUK = false;
                mLocation = null;
            }
        }

        if (isGPSEnabled)
        {
            if( bCoarseOK && bFineOK )
            {
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                        LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, mLocationListener);
                    Location nullTest = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (nullTest != null) mLocation = nullTest; 
        // Sometimes mLocation is null, even though GPS is enabled - in this case,
        // use Network as a fall back option
            }
            else {

                mLocation = null; // no Location provided
            }

        }
    }

}


public void onMapReady(GoogleMap googleMap)
{
    mMap = googleMap;
    mMap.setOnMarkerClickListener(this);

    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    mMap.getUiSettings().setZoomControlsEnabled(true);


  // if mLocation == null, use default location and show on map
  // otherwise show currentLocation on map

  // use mLocation here, but it is null !
}

0 个答案:

没有答案