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.
Related
I have an app that uses MKMapView. In my app, I have declared an array that will hold the response from the API. The data from the API are the jobs to be pinned in the map (clustered annotations). The array that holds the jobs from the API will/needs to be filtered by the jobs pins that are visible in the map. I am able to filter the coordinates (visible or not visible in the map) but I am having troubles on storing the data (coordinates that are visible) in a new array.
Here's what I have so far:
In regionDidChangeAnimated in my mapview
[ar objectAtIndex:0];
NSMutableDictionary *visibleJobs;
for(NSDictionary *loc in ar)
{
CLLocationDegrees Lat = [[[loc objectForKey:#"sub_slots"] objectForKey:#"latitude"] doubleValue];
CLLocationDegrees longTitude = [[[loc objectForKey:#"sub_slots"] objectForKey:#"longitude"] doubleValue];
CLLocationCoordinate2D point = CLLocationCoordinate2DMake(Lat, longTitude);
MKMapPoint mkPoint = MKMapPointForCoordinate(point);
BOOL contains = MKMapRectContainsPoint(mapView.visibleMapRect, mkPoint);
if(contains)
{
NSLog(#"Contains:1");
}
else
{
NSLog(#"Contains:0");
}
}
Any help will be very much appreciated.
Thanks!
why don't you use NSMutableArray
try this
NSMutableArray *visibleJobs = [[NSMutableArray alloc]init];
for(NSDictionary *loc in ar)
{
CLLocationDegrees Lat = [[[loc objectForKey:#"sub_slots"] objectForKey:#"latitude"] doubleValue];
CLLocationDegrees longTitude = [[[loc objectForKey:#"sub_slots"] objectForKey:#"longitude"] doubleValue];
CLLocationCoordinate2D point = CLLocationCoordinate2DMake(Lat, longTitude);
MKMapPoint mkPoint = MKMapPointForCoordinate(point);
BOOL contains = MKMapRectContainsPoint(mapView.visibleMapRect, mkPoint);
if(contains)
{
NSLog(#"Contains:1");
[visibleJobs addObject:loc];
}
else
{
NSLog(#"Contains:0");
}
}
Now visible jobs array contains dictionary of all pin data which in currently visible on map
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).
I need some help with a filter. How do I implement an algorithm that filters out related map annotations within the radius of user's location, thanks a lot guys! Any feedback is appreciated! Thank you and have a nice day.
My code I have so far:
CLLocationCoordinate2D coordinates;
CLLocationDistance test = 1000; // set distance
coordinates.latitude = [[row objectForKey:#"latitude"] doubleValue];// get user's latitude
coordinates.longitude = [[row objectForKey:#"longitude"] doubleValue];
MapAnnotation *annotation = [[MapAnnotation alloc] initWithCoordinate:coordinates title:titles subtitle:contents image:[row objectForKey:#"image"]]; // specify annotation's detail
[self.mapView addAnnotation:annotation];// adding annotation into the map
[annotation release];
I have this situation where my app crashes when I zoom out the map.
The problem arises because of the large number of annotations that I'm adding. Please have a look at my code below :
- (void) plotUsersInMap
{
for (id<MKAnnotation> annotation in self.mapView.annotations) {
[self.mapView removeAnnotation:annotation];
}
NSUInteger count = //get total count
NSLog(#"count * %d", count);
for (int i = 0; i < count; i++)
{
NSNumber *latitude = //get latitude from json
NSNumber *longitude = //get longitude from json
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
#autoreleasepool {
MyLocation *annotation = [[MyLocation alloc] initWithName:#"test" coordinate:coordinate QuestionId:nil];
//annotations are added
[self.mapView addAnnotation:annotation];
}
}
}
Here I'm trying to add more than 400 pins which I think is the cause of crash [probably a memory leak!]. I would like to know if there is any way to add the pins one by one as I zoom out?
Map in initial stage, without any problem :
And when I zoom out :
Try clustering. Basically you group together annotations.
The code repo from the article I linked to: https://github.com/applidium/ADClusterMapView
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.