0

这是我必须在谷歌地图上显示最后一个已知的好位置作为标记的当前代码。一切正常,但我想每隔一段时间(比如 1 分钟或 5 分钟)更新位置。鉴于我正在使用的当前代码,实现这一点的最佳方法是什么?

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class Parking extends Activity implements OnClickListener {

    private GoogleMap map;

    Button b;

    public static final String LATITUDE = "latitude";
    public static final String LONGITUDE = "longitude";

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

        b = (Button)findViewById(R.id.button1);
        b.setOnClickListener(this);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        if (map!=null)
        {       

        //Set Map Type
        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        //Enable MyLocation Layer of Google Map
        map.setMyLocationEnabled(true);

        //Get LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        //Create a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        //Get the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        //Get Current Location
        Location myLocation = locationManager.getLastKnownLocation(provider);

        //Get Lat/Long of the current location
        double latitude = myLocation.getLatitude();
        double longitude = myLocation.getLongitude();

        //Create a latLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        //Show the current location in Google Map
        map.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        //Zoom to location
        map.animateCamera(CameraUpdateFactory.zoomTo(16));

        //Add marker
        map.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .title("You Are Here")
        );




        }//end if map !null
    }
4

2 回答 2

1

您可以使用 LocationManager 类的 requestLocationUpdates 方法 - http://developer.android.com/reference/android/location/LocationManager.html

您可以指定以毫秒为单位的时间间隔以及以米为单位的最小距离变化。

我的建议是切换到最新的 Location API,如下所述:https ://developer.android.com/google/play-services/location.html

于 2013-08-27T20:30:11.457 回答
0

使用来自 Google 的新推出的 Fused Location Providers,获取位置或定期更新变得更加容易!请参阅此链接,这可能非常有帮助

https://developer.android.com/training/location/receive-location-updates.html

您所需要的只是将 google-play 服务导入您的项目,

您也可以使用后台服务来获取定期更新。

public class UpdateLocation extends Service
        implements
            GooglePlayServicesClient.ConnectionCallbacks,
            GooglePlayServicesClient.OnConnectionFailedListener,
            LocationListener {

private static final long UPDATE_INTERVAL = 300000;
private static final int FASTEST_INTERVAL = 60000;
private LocationRequest lr;
private LocationClient lc;
private Location l;

@Override
public void onCreate() {
    super.onCreate();
    lr = LocationRequest.create();
    lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    lr.setInterval(UPDATE_INTERVAL);
    lr.setFastestInterval(FASTEST_INTERVAL);
    lc = new LocationClient(this, this, this);
}


@Override
public void onStart(Intent intent, int startId) {
    lc.connect();

}

@Override
public void onLocationChanged(Location l) {
 //do updates on Location change.
}

@Override
public void onConnectionFailed(ConnectionResult arg0) {

}

@Override
public void onConnected(Bundle arg0) {
    Log.d("AutoTracker : Test", "Location Client Connected");
    if (updates) {
        lc.requestLocationUpdates(lr, this);
    }

}

@Override
public void onDestroy() {
    if (lc.isConnected()) {
        lc.removeLocationUpdates(this);
    }
    lc.disconnect();
}

@Override
public void onDisconnected() {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

}
于 2013-08-27T20:31:42.477 回答