Get user current position with gps/network android MapView - android

Hello I've been using this guide to determine the locationof the user: http://developer.android.com/guide/topics/location/obtaining-user-location.html
and my current code looks like this:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000);
mapController.animateTo(test);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
I only get error and the app shuts down though when I'm using this activity.
Thankful for help

PLease confirm that you have alloted the following permission to your manifest file
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION

I guess you are executing this code in the device
try this on device
Setting -> Location -> Use GPS
AND
Setting -> Application Settings -> Development -> Allow Mock Location

I solved it, I didn't instantiate mapContoller :>. But I've another problem now. which I will post in another question.

Related

Remove one location provider - android

I am listening to 2 location providers
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500,
5,this); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
500, 5, this);
it is possible somehow remove one listener? For example for one task I just want to use GPS update, and while doing this task I want that NETWORK_PROVIDER will be disabled. It is possible somehow? I tried to do like this :
locationManager.removeUpdates(this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5,this);
But this is not really good, because if GPS had fix, this method is removing it and GPS starts to search satellites again.
You can create two different LocationListener:
private LocationListener gpsListener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
if(location != null){
// do your work for gps location
}
}
};
private LocationListener networkListener = .... // same init as gpsListener
And use like:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5, gpsListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 5, networkListener);
Then you can use the approach you are doing, without losing GPS fix.
Instead of:
locationManager.removeUpdates(this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5,this);
You will use:
locationManager.removeUpdates(networkListener);

how to get Longitude and Latitude useing NETWORK_PROVIDER?

when used GPS_PROVIDER the application work fine, but when used NETWORK_PROVIDER the application was stopped by force. Why? are my permissions not complete?? please help me by making my code working, thanx
public class My_location extends Activity implements LocationListener {
EditText ET1,ET2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_location);
// Log.i("onCreate_My_location", "begin");
ET1=(EditText)this.findViewById(R.id.editText1);
ET2=(EditText)this.findViewById(R.id.editText2);
ET1.setText("1.0000");
ET2.setText("1.0000");
LocationManager LM1=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LM1.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,1,this);
//Log.i("onCreate_my_location", "End");
}
#Override
public void onLocationChanged(Location L1) {
// TODO Auto-generated method stub
//Log.i("onLocationChanged",String.valueOf(L1.getLongitude()));
//Log.i("onLocationChanged",String.valueOf(L1.getLatitude()));
//Log.i("onLocationChanged",String.valueOf(L1.getAltitude()));
ET1.setText("C.0000");
ET2.setText("C.0000");
ET1.setText(String.valueOf(L1.getLongitude()));
ET2.setText(String.valueOf(L1.getLatitude()));
}
#Override
public void onProviderDisabled(String arg0) {
}
#Override
public void onProviderEnabled(String arg0) {
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
instead of using specific provider use best provider, which will give you more accuracy in your location
use code given below which will give you location by choosing best provider automatically :
LocationManager LM1=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider=LM1.getBestProvider(criteria, true);
LM1.requestLocationUpdates(provider,0,1,this);
Edit:
for Your Code you Can Check network State before requestLocationUpdates
i think this can avoid the crash :
if(LM1.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
LM1.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,1,this);
}
Check whether the network location provider is enabled or not.
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
ex.printStackTrace();
}
If the provider is enabled then only you can use that. Also, for location updates using a network provider, I think giving the minDistance value as 1 is meaningless as coarse location is anyway not very accurate. You might want to change that value to 50 or 100 meters.

Android Google Maps v2 - OnMyLocationChangeListener - What does it need exactly?

Because I don't want to confuse, I will try to explain with the SDK example-code.
Everything works fine - except the "onMyLocationChange" Callback.
- API 16 & 17 tested
- updated Play Services Rev.5
- Tested with ICS Tablet&Phone
I just added what is needed to receive location-updates:
UiSettingsDemoActivity implements OnMyLocationChangeListener
attached it to the map:
mMap.setOnMyLocationChangeListener(this);
and implemented the callback
#Override
public void onMyLocationChange(Location arg0) {
....
}
But this method is never triggered.
Release Notes from 26. Feb: https://groups.google.com/d/msg/google-maps-android-api-notify/va_IsjNu5-M/QPtoSn69UMgJ - So I thought this is working.
EDIT: http://code.google.com/p/gmaps-api-issues/issues/detail?id=4644
Have you added a LocationListener in your application?:
locationListener = new MyLocationListener();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
MyLocationListener:
public class MyLocationListener implements LocationListener
{
static final String TAG = MyLocationListener.class.getSimpleName();
#Override
public void onLocationChanged(Location location) {
SGTasksListAppObj.getInstance().currentUserLocation = location;
Log.d(TAG, "New location was set to currentUserLocation: "+location.toString());
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Update:
Maybe you should try the:
map.setMyLocationEnabled(boolean boolean);
method, and check this link:
How to get My Location changed event with Google Maps android API v2?
How are you testing whether your location have changed? I would assume it would involve moving around or using dummy data and change the location manually. Does your gps need to be turned on?

Stop LocationManager Updates

I'm trying to have GPS coordinates sent to a text message. This code works, but every time the GPS updates location, it tries to send a new text message. I can't figure out how to stop the location updates. This is what I have currently...I realize it may not be the most efficient way to do it.
Any help would be appreciated!
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public void GPSDisable() {
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.removeUpdates(mlocListener);
}
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "5555555555");
smsIntent.putExtra("sms_body", "Location:"+latitude+","+longitude);
GPSDisable();
startActivity(smsIntent);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
I would set two global variables latitude and longitude.
This variables would be updated every onLocationChanged. You cannot change this comportament, but you can get out from there the sending of sms.
I would send the sms on a timer after 5 seconds let's say.
new java.util.Timer().schedule(
new java.util.TimerTask() {
#Override
public void run() {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "5555555555");
smsIntent.putExtra("sms_body", "Location:"+latitude+","+longitude);
startActivity(smsIntent);
}
},
5000 );
And your onlocation changed could look like this
#Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
First I would advise you to have a look at this article.
Having said this, generally you should "unsubscribe" from the location manager when you do not need any more updates. Typically, you subscribe for location updates in "onCreate" and unsubscribe in "onDestroy" when you need updates for as long as your Activity is 'alive'.
Even better is to subscribe/unsubscribe when your activity is getting visible or hidden respectively (via "onResume"/"onPause" respectively). Have a look here for details on the Activity's lifecycle.
Now, about your specific problem, it is not clear what is your desired behavior. Do you want to send an SMS message every time the location changes? Or only when some time passes?
Assuming what you need is to send SMS whenever your location changes but not too often, I would suggest the following:
1. Register for location updates in "onResume" and unregister in "onPause".
2. Maintain some local variables that tell you which was the last time you sent an SMS message and what were the coordinates in it. These variables should probably persist Activity lifecycle events (e.g. when the Activity is destroyed, you should "store" these values, and restore them when you recreate it).
3. Whenever there is a new location update, check if it is too soon or too close (compared to the previous SMS) and if not send another message, updating the variables.
Having said these, please note that this will work for as long as your Activity is visible. If you need a similar functionality that does not require you to keep the Activity visible, I would suggest you use a BroadcastReceiver.

Android Location in background

i'm new in java/android app and i'm creating an app that uses user-location. I want the location to be updated at the begining of the app.
The problem is that my location class is an activity and i don't want to show another contentview for this class.
Actually, i want the location thing to be done in background, without changing the UI, in a separated class.
Is it possible? How?
Thanks :P
There is no need to put the location in a different activity, the LocationManager already does it in the background:
public void getLocation(){
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gpsLocationListener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
//do something with the new location
if (location != null)
gpsLocation = location;
}
};
gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0, gpsLocationListener);
}
Using the LocationManager you should be able to use what ever kind of activity (or service) you want.

Categories

Resources