Android locationlistener doesn't work on android device
My location listener works when I am using the DDMS controls in the emulator but when I deploy it onto the phone it no longer works. My code is as follows
public class hoosheerAndroidAct extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
MapController mc;
public static MapView gMapView = null;
GeoPoint p = null;
static double latitude, longitude;
MyLocationOverlay myLocationOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
// Creating and initializing Map
gMapView = (MapView) findViewById(R.id.mapview);
p = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
gMapView.setSatellite(true);
gMapView.setBuiltInZoomControls(true);
mc = gMapView.getController();
mc.setCenter(p);
mc.setZoom(11);
myLocationOverlay = new MyLocationOverlay(this, gMapView);
gMapView.getOverlays().add(myLocationOverlay);
list = gMapView.getOverlays();
list.add(myLocationOverlay);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 5.0f,
this);
}
methods implemented for implements LocationListener
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
mc.animateTo(point);
connect("http://api.foursquare.com/v1/venues.json?geolat="
+ location.getLatitude() + "&geolong="
+ location.getLongitude());
Drawable marker = getResources().getDrawable(R.drawable.marker);
gMapView.getOverlays().add(new SitesOverlay(marker));
}
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// required for interface, not used
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stu开发者_如何学Gob
return false;
}
Is there anything I am missing? :-(
Google maps will default to network location provider if the GPS is unable to find a signal so that test could return a location without a GPS signal. I would recommend GPS Status as a good free app. The only other thing I can think of is to make sure you have a clear view of the sky.
its because when your device is not able to find location it return default location 0,0. better you check your device gps connection by checking other apps like map app of your device
Maybe you are testing in door,can't get gps information. try to change LocationManager.GPS_PROVIDER-->LocationManager.NETWORK_PROVIDER.
I had a similar problem and try everything in programming. I read about that google use same other sensors to find location. I even find mobile where location listener won't work in google maps. When I checked the settings i accedently turn on battery power saving and location listener starts. On some other device where location wan't work too I turn on buttery saving and location is shown. I don't know why is that, but buttery maybe will save your app.
精彩评论