is there any way to trigger onLocationChanged other than moving
sameera liyanage
Ranch Hand
Joined: Nov 25, 2008
Posts: 597
posted
0
I need to get current location longitudes and latitudes.But without moving to any location.So is there a way to trigger onLocationChanged method without changing location ?
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2329
posted
0
You will only get updates if there *is* a new location. But you will get an initial "update" with the current location no matter what. If that's not sufficient for your purposes please explain in more detail what you're trying to do.
sameera liyanage
Ranch Hand
Joined: Nov 25, 2008
Posts: 597
posted
0
But you will get an initial "update" with the current location no matter what
1)How it get currentlocation without changes.Can you show with example ?
2)When i run belo codes it alway goes to else part ,without any location changes.Can you tell me the way to go inside in if part without location changes.
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2329
posted
0
How it get currentlocation without changes.Can you show with example ?
Just register for location updates and you will receive at least one, even if you never move.
When i run belo codes it alway goes to else part ,without any location changes.Can you tell me the way to go inside in if part without location changes.
I've already told you in some other thread that there won't necessarily be a last known location. So any code that relies on there being one is doomed to fail.
sameera liyanage
Ranch Hand
Joined: Nov 25, 2008
Posts: 597
posted
0
just register for location updates and you will receive at least one, even if you never move.
please look at below code
when i run this code it gives me nullpointer exception.If it always give a location how it gives null pointer.
The provider has nothing to do with it. lastknownlocation is null, then you call a method on it; consequently you get a NPE.
sameera liyanage
Ranch Hand
Joined: Nov 25, 2008
Posts: 597
posted
0
1)what is the reason for this ?
2)I have buitin Google Map application in my phone.But it never gives me such a error.It always shoe my location.How they built it ?
3)If this happen there is no way to get rid of it.That means we unable to run my application.Isn't there anay solution ?
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2329
posted
1
1)what is the reason for this ?
Not sure what you're asking - why you're getting an NPE if you invoke a method on a null object reference? That's how Java works; surely you have encountered that before.
2)I have buitin Google Map application in my phone.But it never gives me such a error.It always shoe my location.How they built it ?
Probably the same way you're trying to program yours. They're just not invoking methods on null object references.
3)If this happen there is no way to get rid of it.That means we unable to run my application.Isn't there anay solution ?
Of course there's a way to solve this. Why do you think there isn't? Why do you not want to check for null before using an object reference?
sameera liyanage
Ranch Hand
Joined: Nov 25, 2008
Posts: 597
posted
0
Of course there's a way to solve this. Why do you think there isn't? Why do you not want to check for null before using an object reference?
it is ok.But what must do in else part.
other thing is if i use criteria.setAccuracy(Criteria.ACCURACY_COARSE); it doesn't give me null pointer most of time.But if i use criteria.setAccuracy(Criteria.ACCURACY_FINE); most of time it gives me null pointer.Is there any connection with these things with null pointer ?
sameera liyanage
Ranch Hand
Joined: Nov 25, 2008
Posts: 597
posted
0
what is the different between (Criteria.ACCURACY_COARSE) ,(Criteria.ACCURACY_FINE) ?
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2329
posted
1
Let's be very clear about one thing: "it" does not give an NPE. Your code -which is improperly not checking an object reference for null before using it- is reponsible, nobody else is. If you had wanted, you could have gotten rid of the problem long ago, by not accessing lastknownlocation if it is null.
I'm not sure what you mean by "else part", I don't see an if statement in the code you last posted. The underlying problem is very simple: there may not always be a last known location. Specifically, when the app is just starting up, there almost certainly won't. All your code needs to do is to check for that, and act accordingly. For example, by not zooming in on a position. You could show a map of New York instead, just so the user has something to look at.
sameera liyanage
Ranch Hand
Joined: Nov 25, 2008
Posts: 597
posted
0
I need to store last location longitudes & latitudes in my application.So if the lastknowslocation is null i get last location visited and show it.So how to store last visited longitudes & latitudes.
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2329
posted
0
So how to store last visited longitudes & latitudes.
But even then, the very first time the app runs, you will need to decide what to do if there is no last known location. You can't escape this fundamental problem.
sameera liyanage
Ranch Hand
Joined: Nov 25, 2008
Posts: 597
posted
0
I think this way.
I found that we can get location from cell id also.Then first i search for location from GPS.if lastknown location is null then i try with cell id.So then i amable to get a location anyway.Is it correct ?
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2329
posted
0
I very much doubt that a cell/network location will always be available when your app is started. And the user may have that turned off to begin with. Do you really want to require an additional permission on something this uncertain?
It seems that you want to fight tooth and nails the notion that there may not be any location available, while in practice it's all too easy to imagine that just that will be the case. Do you really want your app to fail in those circumstances? Why?
subject: is there any way to trigger onLocationChanged other than moving