I am trying to geocode address into coordinates using following code:
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:#"6138 Bollinger Road, San Jose, United States" completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
NSString *latDest1 = [NSString stringWithFormat:#"%.4f",aPlacemark.location.coordinate.latitude];
NSString *lngDest1 = [NSString stringWithFormat:#"%.4f",aPlacemark.location.coordinate.longitude];
lblDestinationLat.text = latDest1;
lblDestinationLng.text = lngDest1;
}
}];
I have tried it many times but the debugger never enters the block and I am not able to get the location. What can I try next?
All right I found my mistake.
The code is correct and works perfect. All the while I was working on it was through debugger and was trying to figure out why the debugger did not enter the block. But now I have found out debugger does not enter the block at that moment. It takes little in getting the location values. It is done asynchronously so I was not able to find it and I was getting crash because of no values just after the crash. I have moved my code post block to inside the block and everything works fine for me now.
I just ran that exact code and it worked as expected. Make sure that you have an active internet connection.
Try adding a NSLog on the strings and see if it gets called.
NSLog(#"lat: %#, lng: %#", latDest1, lngDest1);
Are you running it in the simulator or the device?
Blocks are new features to Objective C from iOS4.0 onwards. A block you can imagine as a delegate method working in same functional block. As for any delegate method it takes time to invoke, depending upon the condition, same way block executes the code inside it, when it completes its work of geocoding. You can read more about Block in apples documentation or read http://www.raywenderlich.com/9438/how-to-use-blocks-in-ios-5-tutorial-part-2.
You can also have look into my repository on GITHUB https://github.com/Mangesh20/geocoding
Related
I have an application that creates its own live preview prior to taking a still photo. The app needs to run some processing on the image data and thus is not able to rely on AVCaptureVideoPreviewLayer. Getting the initial stream to work is going quite well, using Apple's example code. The problem comes when I try to switch to the higher quality image to take the snapshot. In response to a button press I attempt to reconfigure the session for taking a full resolution photo. I've tried many variations but here is my latest example (which still does not work):
- (void)sessionSetupForPhoto
{
[session beginConfiguration];
session.sessionPreset = AVCaptureSessionPresetPhoto;
AVCaptureStillImageOutput *output = [[[AVCaptureStillImageOutput alloc] init] autorelease];
for (AVCaptureOutput *output in [session outputs]) {
[session removeOutput:output];
}
if ([session canAddOutput:output]){
[session addOutput:output];
} else {
NSLog(#"Not able to add an AVCaptureStillImageOutput");
}
[session commitConfiguration];
}
I am consistently getting an error message just after the commitConfiguration line that looks like this:
(that is to say, I am getting an AVCaptureSessionRuntimeErrorNotification sent to my registered observer)
Received an error:
NSConcreteNotification 0x19d870 {name
= AVCaptureSessionRuntimeErrorNotification;
object = ;
userInfo = {
AVCaptureSessionErrorKey = "Error Domain=AVFoundationErrorDomain
Code=-11800 \"The operation
couldn\U2019t be completed.
(AVFoundationErrorDomain error
-11800.)\" UserInfo=0x19d810 {}";
The documentation in XCode ostensibly provides more information for the error number (-11800), "AVErrorUnknown - Reason for the error is unknown.";
Previously I had also tried calls to stopRunning and startRunning, but no longer do that after watching WWDC Session 409, where it is discouraged. When I was stopping and starting, I was getting a different error message -11819, which corresponds to "AVErrorMediaServicesWereReset - The operation could not be completed because media services became unavailable.", which is much nicer than simply "unknown", but not necessarily any more helpful.
It successfully adds the AVCaptureStillImageOutput (i.e., does NOT emit the log message).
I am testing on an iPhone 3g (w/4.1) and iPhone 4.
This call is happening in the main thread, which is also where my original AVCaptureSession setup took place.
How can I avoid the error? How can I switch to the higher resolution to take the photo?
Thank you!
Since you're processing the video data coming out of the AVCaptureSession, I'm assuming you have an AVCaptureVideoDataOutput connected to it prior to calling sessionSetupForPhoto.
If so, can you elaborate on what you're doing in captureOutput:didOutputSampleBuffer:? Without being able to see more, I'm guessing there may be a problem with removing the old outputs and subsequently setting the photo quality preset.
Also, the output variable you're using as an iterator when you remove your outputs is hiding the still image output. Not a problem, but it makes the code a little harder to read.
There is no need to switch sessions. Just add AVCaptureStillImageOutput to your session on initialization and call the following when you are about to capture the image and use the CMSampleBufferRef accordingly:
captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
}
Hi I'm a new bie in Game Center for iOS. I'm trying to add the multiplayer feature using matches to my game and following the documentation.
So far I reached a point where 2 of my clients can successfully get a match, i.e. the matchmakerViewController:didFindMatch callback is called and a GKMatch object is delivered.
However after that I seems to be stuck there forever, because according to the documentation, I'll have to wait until all the players (2 in my case) are actually connected before starting my game. But it seems the match:player:didChangeState callback is never called to indicate a successful connection. Well, I'm sure my clients are all in the same wifi network ( or is it a must?) Could any one enlighten me on this case? Do I have to do any extra things to make the clients to connect? Thanks a lot for the help!
So I was running into this and the solution (for me) was somewhat embarrasing. I had copied and pasted a bunch of the code from the Apple docs..and they left out an obvious step. They never actually set the match's delegate!
My code now is:
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match {
[self dismissModalViewControllerAnimated:YES];
self.myMatch = match; // Use a retaining property to retain the match.
self.myMatch.delegate = self; // THIS LINE WAS MISSING IN THE APPLE DOCS. DOH.
// Start the game using the match.
NSLog(#"Match started! Expected Player Count:%d %#",match.expectedPlayerCount, match.playerIDs);}
Once I actually set the match delegate, the functions get called. Doh.
When you get the GKMatch object, be sure to check the expectedPlayerCount property. It is possible that the other player is already connected, and thus you will not get a match:player:didChangeState on the delegate.
I have had the same problem with a friend. The solution was quite strange but it works afterwards. On all devices you have to enable the Notifications (Sounds/Alerts/Badges) for the Game Center inside the Settings/Notifications options. Afterwards we could establish a connection and did receive a match object
Inside your callback matchmakerViewController:didFindMatch
Add this code then you'll see the callback "match:player:didChangeState" being called by GC
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 2;
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:request completionHandler:^(NSError* error) {
if(error)
NSLog(#"Error adding player: %#", [error localizedDescription]);
}];
It was working all along. The only difference is that... when you use invites the event "didChangeState" doensn't get called. You're connected without notice and you can start to receive data. I never tried to send/receive data... cuz i was expecting the event first, but i did send something by mistake one time, and it worked. :)
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *) match {
//Dismiss window
[self dismissModalViewControllerAnimated:YES];
//Retain match
self.myMatch = match;
//Delegate
myMatch.delegate = self;
//Flag
matchStarted = TRUE;
//Other stuff
}
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {
//This code gets called only on auto-match
}
The above code works as expected.
Make sure that you've set your class as the delegate for GKSession. The class will need to implement the GKSessionDelegate protocol... otherwise, it'll never receive this callback. Here's the protocol reference. Hope this helps!
I keep getting this randomly when I run my gps app I'm building. It doesn't happen everytime, and the coordinates passed in are always valid (i nslog them). Is there documentation for these somewhere?
EDIT:
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(locManager.location.coordinate.latitude, locManager.location.coordinate.longitude);
geocoder1 = [[MKReverseGeocoder alloc] initWithCoordinate:coord];
geocoder1.delegate = self;
[geocoder1 start];
and then about half the time it returns an error. I tried releasing and re-assigning the geocoder if there was an error, but that didn't help. Only thing that did was restarting the app.
In "MKTypes.h" in the MapKit framework, the following is defined:
Error constants for the Map Kit framework.
enum MKErrorCode {
MKErrorUnknown = 1,
MKErrorServerFailure,
MKErrorLoadingThrottled,
MKErrorPlacemarkNotFound,
};
...
MKErrorPlacemarkNotFound
The specified placemark could not be found.
This sounds like you are referencing some unknown placemark in your code? Or it could be that Google doesn't have a name for the position you are passing - however valid the coordinates may be.
I've met and solved this issue recently. In my case, when Apple Map cannot find any result for a query, it sometimes will just throw this this "MKErrorDomain = 4" error. So I ended up just treat this as "result not found".
It was painstaking to find this out, MapKit needs a better Error handling system.
I've been hitting this error repeatedly, and was unable to figure out how to make it stop; but I finally found an end-run around the whole issue that works quite well, and only takes a little more work: Don't use Apple's MKReverseGeocoder at all -- instead, directly call Google's reverse-geocoding API (this is apparently the same service that MKReverseGeocoder does behind the scenes). You can get back either JSON or XML (your preference), which you will then have to parse, but that isn't too hard.
For example, since my app is using ASIHTTPRequest, this is what it looks like (although this would also be easy to do with do with Apple's native APIs such as NSURLConnection):
#pragma mark -
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// Be careful: My code is passing sensor=true, because I got the lat/long
// from the iPhone's location services, but if you are passing in a lat/long
// that was obtained by some other means, you must pass sensor=false.
NSString* urlStr = [NSString stringWithFormat:
#"http://maps.googleapis.com/maps/api/geocode/xml?latlng=%f,%f&sensor=true",
newLocation.coordinate.latitude, newLocation.coordinate.longitude];
NSURL* url = [NSURL URLWithString:urlStr];
self.reverseGeocoderRequest = [ASIHTTPRequest requestWithURL:url];
self.reverseGeocoderRequest.delegate = self;
[self.reverseGeocoderRequest startAsynchronous];
}
By the way, Google's API has rules, just like Apple's does. Make sure you read the docs, especially regarding quotas.
I'm running into the same thing (the exact same code randomly fails sometimes) and I think I've found the answer. From Apple's developer docs: "Each Map Kit application has a limited amount of reverse geocoding capacity, so it is to your advantage to use reverse geocode requests sparingly."
So my theory is, we're getting rate-limited... since no other variables are changing (i.e. my code isn't changing, I'm running it on the simulator so the location of the device isn't changing, etc.) I think this must be the only remaining reason.
I just got done with a lot of research on this problem and it seems to be outside of our hands. I checked the developer forums as well as all around Stack and elsewhere and no one has a solution other than using a different service. There is a pretty good thread at https://devforums.apple.com/message/154126 on the subject.
Some people find the error after a certain time, I just find it to be out for a while and then comes back. I looked at the "Current Address" sample code and I couldn't see how I might have messed up. I ran the sample code and sure enough, it was NSLogging errors instead of returning a location.
This link has some code using Google's reverse geocoder: http://www.iphonedevsdk.com/forum/iphone-sdk-development/31883-pbrequestererrordomain-errors-reverse-geocoding.html#post155793
Actually, I am running into this problem as well. Code is extremely compact
//1. ask map for current coords
CLLocationCoordinate2D userLocation;
userLocation.latitude = [[_theMapView.userLocation location] coordinate].latitude;
userLocation.longitude = [[_theMapView.userLocation location] coordinate].longitude;
NSLog(#"%f, %f",userLocation.latitude,userLocation.longitude);
//2. reverse geocode coords
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc]
initWithCoordinate:userLocation];
[reverseGeocoder setDelegate:self];
[reverseGeocoder start];
and later simply NSLog the error in the fail delegate method. It works the first time or two, then stops working
Im getting this annoying error and tried every i know but in this case it hasnt helped.
I have in my delegate....
vid_name = [push objectForKey:#"vid"];
(in the console .... vid = "video" )
now in my normal page i have
NSString *videoName = [(MissileAppDelegate *)[[UIApplication sharedApplication] delegate] vid_name];
NSString *path = [[NSBundle mainBundle] pathForResource:videoName ofType:#"mp4"];
when i run this and it comes to play the video, i get an error that doesnt even relate to the video, it happens with all variables i try and pass over using the [push objectForKey:#"vid"];. if i just vid_name =#"video" it works fine.
Any Ideas?
Alex
First of all do not name your variables like vid_name! You should always use vidName in Objective-C.
You should share more information, I'm not really getting what you're doing and the error message you get would be niche.
But It sounds like a memory management problem. Because when you use the #"video" this is a static string that is always there. But when you use objectForKey: you get an object that is autoreleased and get's eventually deallocated at some point. So make sure your vid_name still exists when accessing it from your 'normal page'.
Cocoa Memory Management Programming Guide
Currently i am developing an iPhone application where i have to show user's current city and location for which i am using Mapkit Framework.When i build the application it works fine and show me the exact city details .But right now when i try to build the application again application shows following error message in my log
/SourceCache/ProtocolBuffer_Sim/ProtocolBuffer-26/Runtime/PBRequester.m:523 server returned error: 503
reverseGeocoder: didFailWithError:Error Domain=PBRequesterErrorDomain Code=6001 "Operation could not be completed. (PBRequesterErrorDomain error 6001.)"
Is any body facing the same issue and how can i resolve it?
I am pretty sure this is happening because you are in testing, and using the reverse geocoder too much and Google's servers know that. So, it has basically blocked you for a while. Try again later and see if it works.
You need to make sure that you are not calling the geocoder more than once every 60 seconds.
I find out!
Well, now we'll have to use CLGeocoder, but if you want still to use MKReverseGeocoder, you MUST not call [geocoder start] twice, even if the geocode object is a fresh new one!
For example, DON'T do this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
MKReverseGeocoder *geoCoder = [[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate] autorelease];
geoCoder.delegate = self;
[geoCoder start];
}
Or you'll get an arror: the location manager update position function is being called as soon as a new position is detected. As the accuracy is getting better and better, a new fresh new posistion is being sent to "didUpdateToLocation".
But creating a new object MKReverseGeocoder each time a position is found is a bad idea!
So, you've got a few solutions:
- Put a boolean that is true when geocoder is started. Then in didFindPlacemark (geocoder's), you should stop the geocoder with [geocoder cancel] and set your boolean to false.
- Make geocoder variable not local and manange a [geocoder cancel] + realease and create a new one and[geocoder start] each time didUpdateToLocation is beeing called.
- Any other way to avoid [geocoder start] to be called twice
I used the first solution and never get the 503 error anymore.
For me, MKReverseGeocoder was failing with 503 far too many times. It would also fail the first time it was requested, well after 60 seconds had passed. As a result, I decided to give up using it and instead decided to access Google's API directly using NSURLRequest via a service I created that also parses the result as a JSON String using the SBJSON Framework (very useful).
From my service, I call the following URL as follows:
NSString *urlStr = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", coordinate.latitude, coordinate.longitude];
where coordinate is a CLLocationCoordinate2D.
I realise this is a long way around the problem, but since converting to this system I have had no issues!
It can happen at random, depending on the server's status.