iOS MapView throws a bunch of valueForUndefinedKey - ios

I have a simple map view in an application with a single point annotation. The code is pretty straightforward:
[_mapView removeAnnotations:_mapView.annotations];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
NSString *addressString = [NSString stringWithFormat:#"%#, %#, %#, %#",self.event.address,self.event.city,self.event.state,self.event.zip];
[geocoder geocodeAddressString:addressString completionHandler:^(NSArray* placemarks, NSError* error){
dispatch_async(dispatch_get_main_queue(), ^{
// Assume that the first placemark is the correct address (not sure if there's a better way around this, but in theory should only ever match one).
CLPlacemark *address = [placemarks firstObject];
// add a marker to the map for the address
MKPointAnnotation *addressAnnotation = [[MKPointAnnotation alloc] init];
addressAnnotation.coordinate = address.location.coordinate;
addressAnnotation.title = _event.venue;
[_mapView addAnnotation:addressAnnotation];
MKMapPoint venuePoint = MKMapPointForCoordinate(address.location.coordinate);
// get a default map rect
MKMapRect mapRect = MKMapRectMake(venuePoint.x, venuePoint.y, 0.1, 0.1);
if (showUser) {
// zoom map to include address and user if the user has an address
MKUserLocation *currentUserLocation = [_mapView userLocation];
if (currentUserLocation && currentUserLocation.location) {
MKMapPoint userPoint = MKMapPointForCoordinate(currentUserLocation.location.coordinate);
mapRect = MKMapRectUnion(MKMapRectMake(userPoint.x, userPoint.y, 0.1, 0.1), mapRect);
}
}
// zoom map rect out a little so we can see what's going on here as a user
mapRect = [_mapView mapRectThatFits:mapRect edgePadding:UIEdgeInsetsMake(100, 100, 100, 100)];
[_mapView setVisibleMapRect:mapRect animated:YES];
});
}];
However, when I'm running the app, it throws a bunch of errors that look like this:
<MKPinAnnotationView 0x23dadb90> valueForUndefinedKey:]: this class is not key value coding-compliant for the key subtitle.
The app doesn't crash, and there's no stack trace, so I have no idea where the error is happening or why. There's nothing called subtitle anywhere near this code, I'm assuming the error has something to do with the annotation subtitle?
Not sure where to look for this - I've commented out the annotation code above and the error goes away, so it's somewhat related to this code.

Related

Adding annotations from a KML file to an MKMapView

I am trying to load data from a KML file over to an MKMapView. I was able to parse the data into an array and now I am trying to create annotations on the map for each item.
Using the code below, I was able to create annotations on the map but the location is not correct:
Parser *parser = [[Parser alloc] initWithContentsOfURL:url];
parser.rowName = #"Placemark";
parser.elementNames = #[#"name", #"address", #"coordinates", #"description"];
[parser parse];
//parseItem is an array returned with all data after items are parsed.
for (NSDictionary *locationDetails in parser.parseItems) {
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.title = locationDetails[#"name"];
NSArray *coordinates = [locationDetails[#"coordinates"] componentsSeparatedByString:#","];
annotation.coordinate = CLLocationCoordinate2DMake([coordinates[0] floatValue], [coordinates[1] floatValue]);
[self.mapView addAnnotation:annotation];
}
The result of the NSLog of the coordinates was:
coords=-73.96300100000001,40.682846,0
So it looks like the coordinates are coming in longitude,latitude order but the CLLocationCoordinate2DMake function takes latitude,longitude.
Unless the coordinates are supposed to be in Antarctica instead of New York City, try:
annotation.coordinate = CLLocationCoordinate2DMake(
[coordinates[1] doubleValue],
[coordinates[0] doubleValue]);
Also note you should change floatValue to doubleValue for more accurate placement (it will also match the type of CLLocationDegrees which is a synonym for double).

Is it possible to add pins to map kit solely based on physical address [duplicate]

This question already has answers here:
iOS - MKMapView place annotation by using address instead of lat / long
(7 answers)
Closed 8 years ago.
Is it possible to add pins to map kit solely based on physical address or is there a way to get long and lat based on physical address
Yes, You can get lat long from the address.
Please refer this answer : iOS - MKMapView place annotation by using address instead of lat / long
Basically it does this :
NSString *location = #"some address, state, and zip";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
}
}
];

Address/Zip Code as NSString to CLLocation

Is there a way to convert a zip code a user enters into a textbox and convert it into a CLLocation? I am trying to compare distance between their current location and either an address or zip code and this would be easy if I can make a CLLocation out of the NSString.
the procedure called geocoding, and that is how it looks if you implement it:
NString *_address = // any address or postcode
CLGeocoder *_geocoder = [[CLGeocoder alloc] init];
[_geocoder geocodeAddressString:_address completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count > 0) {
CLPlacemark *_placemark = [placemarks firstObject];
CLLocation *_location = _placemark.location;
// ... do whaterver you want to do with the location
}
}];
NOTE: the CoreLocation.framework has to be added to your project properly. you may need to handle errors in your final completion-block, I have not added such part to my code above.

CLGeocoder returns null results for city and state when using current location data

Hi I am trying to get the name of city and state using CLGeocoder. However, placemark.addressDictionary is returning me :
{
FormattedAddressLines = (
"South Atlantic Ocean"
);
Name = "South Atlantic Ocean";
Ocean = "South Atlantic Ocean";
}
and placemark is:
South Atlantic Ocean, South Atlantic Ocean # <-42.60533670,-21.93128480> +/- 100.00m, region CLCircularRegion (identifier:'<-41.51023865,-31.60774370> radius 4958095.65', center:<-41.51023865,-31.60774370>, radius:4958095.65m)
Also, NSLog of [placemark locality] and [placemark administrativeArea] shows both nil.
Also tried adding ABAdressBookUI , ABAdressBook framework and get the data as:
NSString *addressDict = ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO); which returns an object with empty description.
or tried:
NSString *state = (NSString *)[placemark.addressDictionary objectForKey:kABPersonAddressStateKey]; which throws warning of incompatible pointer types.
If there is any possible solution or if I am missing something please let me know.
My code when current location button is tapped is as follows:
CLLocation *loc;
loc = [[CLLocation alloc] initWithLatitude:[KLocationManager sharedManager]._locationManager.location.coordinate.latitude longitude:[KLocationManager sharedManager]._locationManager.location.coordinate.longitude];
CLGeocoder* geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark* placemark = [placemarks objectAtIndex:0];
if (placemark) {
NSDictionary* dic = placemark.addressDictionary;
NSString* locName = [NSString stringWithFormat:#"%#, %#", [dic objectForKey:#"City"],[dic objectForKey:#"State"]];
CLLocation* loc = placemark.location;
self.returnLocationDict = #{#"name":locName, #"location":loc};
}
}];
What am I missing here?
One way of replicating what you're experiencing is to use a CLLocation with latitude and longitude of 0,0
I ran your code by using latitude:37.3318 longitude:-122.0312 (Infinite Loop..) and it worked as expected.
Your location manager is probably not returning what you expect.

iOS MapView only adding first annotation

I have an array named allposts that has 4 objects in it. When I run the loop below, only the first annotation is put onto the map. I print out the latitude and longitude of each "point" and they are indeed different, so Im not sure whats going on
for (PFObject *post in _allPosts)
{
NSLog(#"NEWANNOTATION");
PFGeoPoint *point = [post objectForKey:#"Location"];
NSLog(#"lat: %f long: %f", point.latitude, point.longitude);
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
CCBAnnotation *annotation = [[CCBAnnotation alloc] initWithCoordinate:coordinate];
[self.mapView addAnnotation:annotation];
}
You should also inspect mapView.annotations after doing this in order to determine if it's an adding problem or a display problem.

Resources