簡體   English   中英

如何獲取當前的 GPS 位置?

[英]How can I get the current GPS location?

我知道以前有人問過這個問題,但是所有可用的解決方案都要求我使用onLocationChanged()方法,以便我可以使用getLatitude()getLongitude()方法來獲取坐標。

但是onLocationChanged()會在我的設備位置改變時被調用。 因此,為了獲得我當前的位置,我必須移動我的設備。

但我想獲得當前坐標而不必移動我的設備。 我還在某處讀到我可以手動調用onLocationChanged()方法,但我可以再次獲取最后一個已知位置。 最后一個已知位置不是我當前的位置嗎? 還是可以使用最后一個已知的位置解決方案?

這是我用來實現 LocationListener 的類的一部分。

public class MyLocListener extends MapsActivity implements LocationListener {

    public void onLocationChanged(Location location)
    {
        if(location!=null)
        {
            MapsActivity.setLatitude(location.getLatitude());
            MapsActivity.setLongitude(location.getLongitude());
        }
    }}

這是我用來為我的地圖活動提供坐標的函數的覆蓋。 緯度和經度是兩個雙精度型變量。 在這個類中,緯度和經度變量從 0 開始,它們保持這種狀態,因為onLocationChanged()函數只在我的位置改變時調用。 因此我無法在地圖上看到我當前的位置。

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;


        LocationManager myLocManager;

        myLocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        MyLocListener loc = new MyLocListener();
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
        }
        myLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);

        LatLng sydney = new LatLng(latitude, longitude);

        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

你應該使用 GooglePlayServices

compile 'com.google.android.gms:play-services-location:7.0.0'

獲取位置

 if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
}
if (mGoogleApiClient != null) {
    mGoogleApiClient.connect();
}

連接后

public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {
    ...
    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }
}

有關更多詳細信息,請查看文檔

移動你的代碼里面的位置改變了。 由於獲取位置是異步的,因此您的緯度和經度永遠不會出現反映。 根據需要優化初始化。

public class MyLocListener extends MapsActivity implements LocationListener {

public void onLocationChanged(Location location)
{
    if(location!=null)
    {
        MapsActivity.setLatitude(location.getLatitude());
        MapsActivity.setLongitude(location.getLongitude());

    LatLng sydney = new LatLng(location.getLatitude(), location.getLongitude);

    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    }
}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM