使用谷歌位置API获取Android中onMapReady的当前位置

时间:2016-04-16 00:58:30

标签: android google-maps-android-api-2

我正在尝试在我的应用内的谷歌地图上显示用户的当前位置,但我不想在用户移动时不断更新位置。他的初始位置应该被记录并显示,直到他关闭应用程序。我为此编写了以下代码:

private GoogleMap mMap;
protected GoogleApiClient mGoogleApiClient;
Location mLastLocation;
double lat =0, lng=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    buildGoogleApiClient();

    // 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);


}

private void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}


/**
 * 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;

    // Add a marker in Sydney and move the camera
    mMap.setMyLocationEnabled(true);

    LatLng loc = new LatLng(lat, lng);
    mMap.addMarker(new MarkerOptions().position(loc).title("New Marker"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
}

@Override
public void onConnected(Bundle bundle) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        lat = mLastLocation.getLatitude();
        lng = mLastLocation.getLongitude();
    }
}

@Override
protected void onStart() {
    super.onStart();

    mGoogleApiClient.connect();
}

此代码的问题是我在lat, lng函数已经完成执行后得到onMapReady。我认为纠正这种情况的方法之一是创建AsyncTask以确保在地图之前检索位置数据。但是,我试图以不使用AsyncTask的方式实现这一点。

1 个答案:

答案 0 :(得分:6)

只需将创建标记的代码从onConnected()移至buildGoogleApiClient(),然后将onCreate()来电从onMapReady()移至@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); //buildGoogleApiClient(); // 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; // Add a marker in Sydney and move the camera mMap.setMyLocationEnabled(true); //add this here: buildGoogleApiClient(); //LatLng loc = new LatLng(lat, lng); //mMap.addMarker(new MarkerOptions().position(loc).title("New Marker")); //mMap.moveCamera(CameraUpdateFactory.newLatLng(loc)); } @Override public void onConnected(Bundle bundle) { mLastLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); if (mLastLocation != null) { lat = mLastLocation.getLatitude(); lng = mLastLocation.getLongitude(); LatLng loc = new LatLng(lat, lng); mMap.addMarker(new MarkerOptions().position(loc).title("New Marker")); mMap.moveCamera(CameraUpdateFactory.newLatLng(loc)); } }

getLastLocation()

虽然,请注意,您在requestLocationUpdates()的通话中经常会收到空。您可以使用removeLocationUpdates(),并在第一个位置进入时致电{{1}}。 看看this answer,它有一个完整的例子,说明你正在尝试做什么。