Location not being updated on new geo fix in android emulator - java

I am trying to make it so a MapView zooms to a current location based on a GeoPoint. I am setting the location using the geo fix command in telnet. My problem is that when I first input a location using geo fix my code will correctly navigate to a location on the map. If I try to set another location using geo fix however it does not update. Here is the code to update:
public void updateLocation(Location loc) {
p = new GeoPoint((int)(loc.getLongitude() * 1E6),(int)(loc.getLatitude() * 1E6));
mc = mapView.getController();
mc.animateTo(p);
}
and here is my code to call the update:
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location);
}
etc...
I have the following in onResume():
super.onResume();
myLocationManager.requestLocationUpdates("gps", 0, 200, onLocationChange);
The points I am trying to geo fix to are far enough apart to meet the minimum distance requirement. Anyone have any ideas of what I'm missing?

Call mapView.invalidate(); (see View:invalidate()) or mapView.postInvalidate(); (see View:postInvalidate()), depending on if it runs on UI thread or not, after
mc.animateTo(p);

Assuming your MapActivity implements LocationListener, then change
myLocationManager.requestLocationUpdates("gps", 0, 200, onLocationChange);
to
myLocationManager.requestLocationUpdates("gps", 0, 200, this);
'this' holds the location listener

Related

Android refresh google map after turning on location

I have a google map in my project. First of all, I check whether the user has location enabled. If the user does not have location enabled, a dialog box pops up asking them to enable location. When the user accepts this prompt, he or she is redirected to the settings page where they can enable location.
The problem is after enabling location and press back, the map remains in its previous state, i.e. does not zoom to the user's current location.
How can I solve this?
I think you should reload the map on the method onResume:
edit (i am assuming that you have declared the mGoogleMap object on class scope):
GoogleMap googleMap;
#Override
public void onResume() {
super.onResume();
if(googleMap != null){
googleMap.clear();
// add the markers just like how you did the first time
}
}
You have to implement LocationListener
public class FragmentMap extends SupportMapFragment implements LocationListener
{
GoogleMap mGoogleMap_;
[...]
#Override
public void onLocationChanged(Location location)
{ CameraPosition.Builder builder = CameraPosition.builder(mGoogleMap_.getCameraPosition());
builder.target(new LatLng(location.getLatitude(), location.getLongitude()));
mGoogleMap_.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build())); // CameraUpdateFactory.newLatLngZoom(...) if you need to zoom too
}
[...]

Android GPS get driving car speed

How do my android cellphone get the a driving car speed from GPS?
Can you give me an example which it can get car location from GPS and get speed?
Any advice is a help.
Thank you very much.
The location has an attribute speed which you get by getSpeed() method.
The GPS receiver chip determines speed NOT by distance and time relation.
It uses the Doppler Shift Effect, which gives more precise speed.
The higher the speed, the more acurate it is. Under 10km/h it does not work well.
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
location.getLatitude();
Toast.makeText(context, "Current speed:" + location.getSpeed(),
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
}
Using android locationmanager you can get latest last known location and in Location class getSpeed() method is there which will gve speed
the speed if it is available, in meters/second over ground.
If this location does not have a speed then 0.0 is returned.
Another way is find the distance between two location using location manager & provider and Location has one method to get distance in meter distanceTo(Location)
Convert Distance to meter and get the time From Location Class using getTime() and apply speed formula (speed = distance/time)

Location updates not showing for GPS

I'm trying to retrieve the device's current location using the device's GPS. When I register for updates (as shown below) for LocationManager.GPS_PROVIDER no updates are rolling, yet when I do the same for LocationManager.NETWORK_PROVIDER I get the updates.
I have tested it on my Android device (not the emulator). I have also made sure the GPS is on. Other apps (not mine) are able to access my location. What am I missing here?
LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
locationRetrieved(location, this);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { providerDisabled(); }
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
GPS start up is not an instant process. The receiver basically has to know the position of each of the GPS satellites in the constellation in order to calculate it's location
Are you first checking if there is a last known location which you can use? Most apps first check for it and then search for a location.
You should check wheter your phone is allowing the gathering of location with GPS.
Probabily what is happening is that in Settings -> Location you only have the checkbox for wireless location. If so, the GPS_PROVIDER doesn't work.

Waiting for GPS location before proceeding Android

I am aware there are a number of similar questions floating around but could not find a suitable answer in my searches.
My App has an activity which relies on the GPS location to function. The app will crash if it is run immediately after start up due to null pointer exceptions etc where the location is null. I am aware these are my programming errors and I should handle the exceptions, but the activity is still useless until a GPS fix is found.
When the app crashes I get the "Searching for GPS" icon, and if this is left for long enough a GPS location is found and the app can be run with no issue.
I have created another activity to act as a home screen which will enable the button to link to the above activity when a GPS location is found. The problem is that it doesn't seem to find or be looking for a GPS signal. By this i mean that no "Searching for GPS" icon appears and the button is never enabled. The code for this home activity is below, have I missed something?
My activity implements LocationListener, and the code below is in the Activity "onCreate" method. "startExplore" starts my GPS reliant activity.
exploreButton = (Button) findViewById(R.id.button1);
exploreButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startExplore();
}
});
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
exploreButton.setEnabled(false);
exploreButton.setText("Finding location . . .");
}
This is my "onLocationChanged" method. As i said above, unlike my other activity I do not get the "Searching for GPS" icon and the location does not appear to be found despite this being the same method used in my other Activity. So is this the correct technique for waiting until a location is found, and can anyone identify any mistakes?
public void onLocationChanged(Location location) {
exploreButton.setEnabled(true);
exploreButton.setText("Found signal");
}
Call the
locationManager.requestLocationUpdates(locationProvider, 0, 0, this)
to get the updated location.. It might be the source of the problem.. Because of this only "Searching for GPS" wasn't initiated I think..
Move it out the onCreate methode or put it in a handler or so.
Example:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//methods you want.
}
}, 100);

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 stub
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.

Categories

Resources