我在我的项目中使用 novoda ( https://github.com/novoda/Novocation ) 外部位置 jar,我将更新间隔设置为半分钟,并且我使用地图中的定位器类来显示用户位置,因此,在尝试在外面使用应用程序时,我可以在地图上获得我的第一个位置,但之后它似乎不再“跟踪”我的位置 - 即用户覆盖不会根据给定时间重新定位其在地图上的位置间隔(30 秒) - 有人使用同一个项目并有建议吗?这是我的代码:
package com.mappermate.ui.phone;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.Overlay;
import com.novoda.location.Locator;
import com.novoda.location.exception.NoProviderAvailable;
import com.readystatesoftware.maps.OnSingleTapListener;
import com.readystatesoftware.maps.TapControlledMapView;
import com.mappermate.R;
import com.mappermate.application.MappermateApp;
import com.mappermate.logic.config.MappermateConfig;
import com.mappermate.logic.gps.GPSTracker;
import com.mappermate.logic.mapmenu.MapSearchMenu;
import com.mappermate.logic.mapoverlays.CustomItemizedOverlay;
import com.mappermate.logic.mapoverlays.CustomOverlayItem;
import com.mappermate.logic.mapoverlays.SimpleItemizedOverlay;
import com.mappermate.logic.mapoverlays.UserItemizedOverlay;
import com.mappermate.logic.mapoverlays.UserOverlayItem;
import com.mappermate.logic.net.AlertDialogManager;
import com.mappermate.logic.net.ConnectionDetector;
public class MyLocationActivity extends MapActivity implements LocationListener{
private TapControlledMapView m_MapView; // use the custom
// TapControlledMapView
private List<Overlay> m_MapOverlays;
Drawable redCircle;
Drawable greenCircle;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlayWithPicture;
SimpleItemizedOverlay itemizedOverlayWithoutPicture;
/**
* Added after original
*/
// Checkers to validate user have an open network connection
// And an active GPS
private ConnectionDetector m_ConnectionDetector;
private AlertDialogManager m_Alert = new AlertDialogManager();
// User overlay
private double m_UserLongitude;
private double m_UserLatitude;
private Drawable m_UserLocationMarker;
private GeoPoint m_UserGeoPoint;
private UserOverlayItem m_UserOverlayItem;
private UserItemizedOverlay m_UserItemizedOverlay;
// Class variables
private static ImageButton m_MapMenuButton;
private GPSTracker m_GPS;
private boolean m_WorkOffline;
private final static int ZOOM_LEVEL = 17;
private MapController m_MapController;
private static final int USER_OVERLAY = 0;
private static final int PLACES_OVERLAY = 1;
private MapSearchMenu m_MapSerachMenu;
private final int TRANSPERENT = 150;
private final int OPAQUE = 255;
private View m_View;
// Added those for locator jar file
Locator m_Locator;
MappermateApp m_MappermateApp;
@Override
public void onCreate(Bundle savedInstanceState) {
// Inflate the map with the given xml and return to saved instance in
// Case there is such one
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// Get the reference to the locator object
m_MappermateApp = (MappermateApp) getApplication();
m_Locator = m_MappermateApp.getLocator();
// Initiate class fields
InitiateMapFields();
// Get the map and set the zoom controls to on
m_MapView = (TapControlledMapView) findViewById(R.id.mapview);
m_MapView.setBuiltInZoomControls(false);
// Get the map overlays - to place objects on
m_MapOverlays = m_MapView.getOverlays();
// Place user on map
initUserLocationOnMap();
if (savedInstanceState == null) {
m_MapController = m_MapView.getController();
m_MapController.setZoom(ZOOM_LEVEL);
if (m_UserGeoPoint != null) {
m_MapController.animateTo(m_UserGeoPoint);
}
} else {
// restoring focused state of overlays
int focused;
focused = savedInstanceState.getInt("focused_1", -1);
if (focused >= 0) {
itemizedOverlayWithPicture.setFocus(itemizedOverlayWithPicture
.getItem(focused));
}
focused = savedInstanceState.getInt("focused_2", -1);
if (focused >= 0) {
itemizedOverlayWithoutPicture
.setFocus(itemizedOverlayWithoutPicture
.getItem(focused));
}
}
}
/***********************************************************
***********************************************************
**************** Initiate Class Members *******************
***********************************************************
***********************************************************/
private void InitiateMapFields() {
// Init the classes
m_ConnectionDetector = new ConnectionDetector(getApplicationContext());
m_GPS = new GPSTracker(MyLocationActivity.this);
// Set the map buttons images their listeners
m_MapMenuButton = (ImageButton) findViewById(R.id.show_map_menu);
m_MapMenuButton.setAlpha(TRANSPERENT);
}
/*******************************************
******************************************
******** User overlay methods ************
******************************************
******************************************/
private void initUserLocationOnMap() {
// User overlay on map
m_UserLocationMarker = getResources().getDrawable(
R.drawable.user_location);
m_UserItemizedOverlay = new UserItemizedOverlay(m_UserLocationMarker,
m_MapView);
// set OS behavior attributes for overlay
m_UserItemizedOverlay.setShowClose(true);
// m_UserItemizedOverlay.setShowDisclosure(true);
m_UserItemizedOverlay.setSnapToCenter(true);
}
/**
* Add the current user location to the map
*
* @param i_GeoPoint
* - The user current location
*/
public void addUserLocationAndInvalidate(GeoPoint i_GeoPoint) {
m_MapOverlays = m_MapView.getOverlays();
if (m_MapOverlays.contains(m_UserItemizedOverlay)) {
m_MapOverlays.remove(m_UserItemizedOverlay);
}
m_UserOverlayItem = new UserOverlayItem(i_GeoPoint);
m_UserItemizedOverlay.addOverlay(m_UserOverlayItem);
m_MapOverlays.add(m_UserItemizedOverlay);
m_MapView.invalidate();
m_MapController.animateTo(i_GeoPoint);
Toast.makeText(this, "Changed", Toast.LENGTH_SHORT)
.show();
}
/************************************************************************
************ Default inherited methods and menu methods ****************
************************************************************************/
// Saving state of overlays
@Override
protected void onSaveInstanceState(Bundle i_OutState) {
super.onSaveInstanceState(i_OutState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MappermateApp.LOCATION_UPDATE_ACTION);
registerReceiver(m_FreshLocationReceiver, intentFilter);
try {
m_Locator.startLocationUpdates();
} catch (NoProviderAvailable i_NoProviderAvailable) {
Toast.makeText(this, "No provider available", Toast.LENGTH_LONG)
.show();
m_UserGeoPoint = getLastKnownLocation();
}
}
private GeoPoint getLastKnownLocation() {
GeoPoint geopoint = null;
Location location;
long bestTime = -1;
boolean hasResult = false;
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = locationManager.getAllProviders();
if (providers != null) {
for (String provider : providers) {
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
hasResult = true;
if (location.getTime() > bestTime) {
geopoint = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
bestTime = location.getTime();
}
}
}
}
if (hasResult)
return geopoint;
return null;
}
public BroadcastReceiver m_FreshLocationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
m_UserLatitude = m_Locator.getLocation().getLatitude();
m_UserLongitude = m_Locator.getLocation().getLongitude();
m_UserGeoPoint = new GeoPoint((int) (m_UserLatitude * 1E6),
(int) (m_UserLongitude * 1E6));
addUserLocationAndInvalidate(m_UserGeoPoint);
}
};
@Override
public void onPause() {
unregisterReceiver(m_FreshLocationReceiver);
m_Locator.stopLocationUpdates();
super.onPause();
}
@Override
public void onLocationChanged(Location location) {
addUserLocationAndInvalidate(new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6)));
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}