Blackberry - getLocation() method cannot be called from event thread - blackberry

I want to get the longitude and latitude of device. I used location api to get the longtitude and latitude the problem now I am facing is that I could not call the getLocation() method inside button click event. It throws an error as getLocation() method cannot be called from event thread.Can any one tell me how to solve this problem?

You simply create another thread and call getLocation() from that thread. See the documentation for java.lang.Thread in the API documentation.

Related

Completion block for GPS coordinates

As my apps enables the user to get their location every now and then, I could really use the ability to get the location in a completion block. At the moment I've set up a notification using
NSNotificationCenter.defaultCenter().postNotificationName("functionToRun", object: nil)
When the user request the location, I just run this function
func getLocation() -> CLLocation {
locationManager.requestLocation()
return location
}
However this is updating the location which can take a while, and just returning the latest location, can I implement this in a completion block, so I can get the actual location?
CLLocationManager doesn't exactly work this way but you could easily use something like the CLLocationManager-blocks cocoa pod to get this functionality. Just read through the documentation to make sure it behaves the way you expect it to.

iOS reverseGeocodeLocation error [duplicate]

I have a search bar in my application that the user can type an address into, and it will come up with the geocoded result. The result updates as the user types, according to the following code:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
...
if (self.geocoder.geocoding) [self.geocoder cancelGeocode];
[self.geocoder geocodeAddressString:searchText completionHandler:^(NSArray *placemarks, NSError *error) {
if (error != nil) {
NSLog(#"ERROR during geocode: %#", error.description);
return;
}
//update the view
}];
}
This works for the first few characters the user enters into the search field. However, after the user types more characters repeatedly, the geocoder starts giving the following error (which I know means that there was a problem with the network):
ERROR during geocode: Error Domain=kCLErrorDomain Code=2 "The operation couldn’t be completed. (kCLErrorDomain error 2.)"
The geocoder does not work again until the entire ViewController is reloaded. Why could this be happening, and what can I do to resolve it?
I believe the reason is the following:
Apple's geocoder does not answer every request in the same way. Instead, the first requests from a certain device are answered quickly, but if the device has sent say 100 requests or more, the answers arrive slower and slower or requests are not answered at all, which might cause your error.
When you reload the view controller, this simply takes time, and the geocoding server is more willing to answer again.
Essentially, you cannot do anything about it, since the geocoder sever wants to protect itself from being overloaded by requests from a single device. You simply had to limit the number of requests that you send there.
BTW: The docs say "you should not send more than one geocoding request per minute".
Note that this same error is returned when the device is offline.
I had this problem while picking location for messenger application.
My solution was to introduce delay of 3 seconds, after user stop panning map, before geocoder call. To ensure that user want exactly that location.
I was using 3 delegate methods
func mapView(_ mapView: GMSMapView, willMove gesture: Bool)
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition)
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition)
And I was calling the reverse geolocation API in each of the methods. I got triggered the error message.
The error mainly because you are requesting the reverse geolocation API multiple times and more frequently.
How?
-> When you are about to start dragging, the first delegate method fires
-> When I was dragging the view, the camera is being changed, so the second delegate method is being fired and requesting geolocation API
-> When the camera is idle, the third delegate method is fired.
For my case, I had to show the location data in a label, like Uber set on the map, and I analyzed I need the data actually when the camera position is idle. Like I want to get the data of 10KM distance place, do I need the intermediate 9KM data?
so I removed the geolocation call from the first and second delegate method and kept only in the 3rd one. I was setting Loading.. in the label when the delegate methods got fired.
Fetching data in the background thread, because I don't want to hang up the main thread for this.
Also kept a 1-second delay before fetching, just for keeping a separation between the 2 API calls.

Get current location from the geo fence sample project

I was trying out the Google's Geo fence sample project. I'm getting the entry/exit events correctly. But how can I get the current location of the user from it. I would like to keep track of user location even after entry/exit points. Please help.
The sample project is from the developer portal.
in your broadcastrecievero or service you get a GeofencingEvent , use it to get the triggering position
public class GeofenceTransitionsIntentService extends IntentService {
//[...]
#Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
// ...
}
Location l = geofencingEvent.getTriggeringLocation()
}
//[...]
}
You'll need can get the user location from the Fused Location Provider in the normal fashion. The full tutorial is here: http://developer.android.com/training/location/retrieve-current.html
But basically, when you receive the geofence event inside your Receiver or Service, get an instance of LocationClient there, connect to it, and in the callback, onConnected(), get the last known location:
Location position = locationClient.getLastLocation();
99.9% of the time, or a little better, you'll find this location to be inside your geofence.
To continue keeping track of user location updates, follow the Receiving Location Updates: http://developer.android.com/training/location/receive-location-updates.html

Get user's current location in Xamarin iOS

I would like to get the user's current location in iOS Xamarin. Here is my code.
CLLocationManager locationManager = new CLLocationManager();
locationManager.StartUpdatingLocation();
locationManager.StartUpdatingHeading();
locationManager.LocationsUpdated += delegate(object sender , CLLocationsUpdatedEventArgs e )
{
foreach(CLLocation loc in e.Locations)
{
Console.WriteLine(loc.Coordinate.Latitude);
}
};
1.When i launch my app, a dialog "AppName would like to use your current location" pops in and after one or two seconds it dismisses by itself(before i could click any button). Why is this happening?
2.
App works fine when i manually(settings->privacy->locations) enable location services for my app. I want this to happen programmatically. I have checked this post also similar question.Where am i going wrong?
Please help
Is this for iOS8? Did you call RequestWhenInUseAuthorization?
As for the dismiss of the dialog, that is most likely due to the locationManager variable not being declared at the class level.

Suppress RestKit Error Message

Using RestKit 0.20.x I am firing off a series of API calls on a particular view. If the user sends the app to the background I am cancelling ALL API calls using the following statement in the app delegate
- (void)applicationDidEnterBackground:(UIApplication *)application{
[self.weatherManager.restKitManager.operationQueue cancelAllOperations];
}
When the app returns from the background a UIAlert is visible with the following message:
The operation could not be completed. (org.restkit.RestKit.ErrorDomain error 2).
I don't want the user to have to see or dismiss this message upon return but am having trouble figuring out where/how to suppress this message.
Initially I was avoiding the use of cancelAllObjectRequestOperationsWithMethod: matchingPathPattern: because I had found a post on Google Groups from Blake Watters suggesting the use of the cancelAllOperations method on the operation queue which did not require a pathPattern
However If I use
[self.weatherManager.restKitManager cancelAllObjectRequestOperationsWithMethod:RKRequestMethodAny matchingPathPattern:#"/"];
by specifying a path pattern of just a "/" this seems to do the trick.

Resources