I am wondering how I might be able to get the placemark's zip code in iOS. Any tips or suggestions will be deeply appreciated. Here is the code I have so far, I am able to get the city.
- (void)recenterMapToPlacemark:(CLPlacemark *)placemark {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
region.span = span;
region.center = placemark.location.coordinate;
NSString *city = [placemark locality];
}
Check the documentation here:
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLPlacemark_class/Reference/Reference.html#//apple_ref/occ/instp/CLPlacemark/postalCode
It is the postalCode property you are looking for.
So in your case it is:
NSString *zipCode = placemark.postalCode;
Related
so I'm trying to search a location using CLGeocoder,
It works the first time I do it, but it's very unreliable. Sometimes it will work, when I go back, try it again. crashes. Sometimes when I load it I get an error PBRequester failed with Error Error Domain=NSURLErrorDomain, then something with the span.
This is my code:
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:placeName completionHandler:^(NSArray *placemarks, NSError *error) {
//Error checking
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
region.center = [(CLCircularRegion *)placemark.region center];
region.center.latitude = placemark.region.center.latitude;
region.center.longitude = placemark.region.center.longitude;
MKCoordinateSpan span;
double radius = placemark.region.radius / 1000; // convert to km
NSLog(#"Radius is %f", radius);
span.latitudeDelta = radius / 112.0;
region.span = span;
[self.mapView setRegion:region animated:YES];
}];
During geocoding a lot of things can happen. Since it is an internet service its functionality is based on internet reachability and available band.
Fortunately the CLGeocoder returns and error in the completion block, it is supposed that if an error happen you must handle correctly. If your app crashes is not a geocoded fault, because probably you are not handling well the error or the situation.
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:placeName completionHandler:^(NSArray *placemarks, NSError *error) {
/* Error checking */
if (error)
{
NSLog(#"The error is %#",error.localizedDescription);
return;
}
if (placemarks.count >1)
{
/* Handle multiple placemarks */
}
else {
/* This is probably the source of your crashes if there is an error placemarks it would be an empty array, but if you ask for an object it will raise an exception for out-of-bounds index */
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
region.center = [(CLCircularRegion *)placemark.region center];
region.center.latitude = placemark.region.center.latitude;
region.center.longitude = placemark.region.center.longitude;
MKCoordinateSpan span;
double radius = placemark.region.radius / 1000; /* convert to km */
NSLog(#"Radius is %f", radius);
span.latitudeDelta = radius / 112.0;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
}];
Some suggestion:
Handle the error
Check how many placemarks the geocoded have be returned, they can be more than one
In your code if the geocoded will return an empty array, asking for an object at index 0 will raise an out-of-bounds exception, always check if the count is different from 0 or use -firstObject method (iOS7 only)
This question already has answers here:
iPhone: Create MKAnnotation
(3 answers)
Closed 8 years ago.
How to set marker in MKMapView
Here is my code to set a latitude and longitude but i dont know how to set marker
please give me solution.
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
MKCoordinateRegion region;
MKCoordinateSpan span;
location.latitude = center.latitude; //37.250556;
location.longitude = center.longitude; //-96.358333;
span.latitudeDelta = 0.05;
span.longitudeDelta = 0.05;
region.span = span;
region.center = location;
[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];
Useful tutorial: maybelost.com/2011/01/a-basic-mapview-and-annotation-tutorial
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:#"Buckingham Palace" andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
[newAnnotation release];
Could you point me what is wrong with this code?
-(void) showStoreRegion:(NSInteger)idx
{
//StoreLocation* store = [self.listStore objectAtIndex:idx];
//self.coordinate = CLLocationCoordinate2DMake(store.latitude,store.longitude);
self.coordinate = CLLocationCoordinate2DMake(10.7500,106.6667);
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.001;
span.longitudeDelta = 0.001;
region.span = span;
region.center = self.coordinate;
[theMapView setRegion:region animated:TRUE];
[theMapView regionThatFits:region];
[self addAnns];
}
I got this message "terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region '
" when run this code/
You haven't initialised your region propertly. Try this
MKCoordinateSpan span = MKCoordinateSpanMake(0.001,0.001);
MKCoordinateRegion region = MKCoordinateRegionMake(self.coordinate, span)
I've been adding MapView into my ViewController. Code as below:
MapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
MKCoordinateRegion region;
region.center.latitude = [[myStore.Location objectAtIndex:0] doubleValue];
region.center.longitude = [[myStore.Location objectAtIndex:1] doubleValue];
MKCoordinateSpan span;
span.latitudeDelta = .0015;
span.longitudeDelta = .0015;
region.span = span;
[MapView setRegion:region animated:YES];
myStore.Location is an array with coordinates. XCode just crashes the app and return the error at [MapView setRegion:region animated:YES]; which i assume that it cannot init the mapview, can anyone help?
i've resolved my problem... the latitude and longitude was being reversed. [myStore.Location objectAtIndex:0] should be the longitude and [myStore.Location objectAtIndex:1] should be the latitude... silly me.
I am trying to display some location in MKMapView. While loading the mapViewController, I am getting the following error message in console.
__GEORegisterNetworkDefaults_block_invoke_0: Could not connect to geod on com.apple.geod.defaults
I am using xcode 4.3 and iOS 5.1 for my application.
I am using the following code to show the address:
- (void) showAddress
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
CLLocationCoordinate2D location = [self addressLocation];
region.span = span;
region.center = location;
}
And for getting the address location from following code.
-(CLLocationCoordinate2D) addressLocation
{
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[#"cupertino" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *err;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:&err];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"])
{
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
When I google about the mapkit tutorials then I found the above code from http://mithin.in/2009/06/22/using-iphone-sdk-mapkit-framework-a-tutorial
Does anyone know why I am getting the above error?
Have you told XCode that your app requires access to location information? I think it's 'privileges' or something similar in the project config.