filter map annotations by distance - ios

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];

Related

Avoid overlapping of google map marker using clustering

There are thousands of data that I want to add on google map using clustering.
Problem
It is possible that multiple records may have same latitude and longitude so I want to add those marker in such a way that it doesn't overlap one another. They can be close to each other but not overlapped.
So I have went through the google place api documentation and even googled or the various articles but I didn't found a solution that solves this issue.
Setting pin on map:-
- (void)setPinsToMap {
[self.mapGoogle clear];
for (int i=0; i<arrUsers.count; i++) {
ClassUser *cls =[arrUsers objectAtIndex:i];
double lat;
double long1;
//Here, what to do with the location coordinates?
lat = [cls.strLat doubleValue];
long1 = [cls.strLong doubleValue];
NSLog(#"\n------------------\nname = %# lat = %#, lng = %#\n---------------\n", cls.strUserName, cls.strLat, cls.strLong);
NSString *name = [NSString stringWithFormat:#"%d", i];
id<GMUClusterItem> item = [[POIItem1 alloc] initWithPosition:CLLocationCoordinate2DMake(lat, long1) name:name];
[_clusterManager addItem:item];
[_clusterManager cluster];
}
}
Could any one suggest a way to get this fixed. Any help will be welcomed.

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).

iOS : App crashes when zooming out a map

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

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.

Plotting multiple markers in Google Maps

Hi I am working on Google Maps SDK for ios. I want to plot a number of markers in Google maps from NSArray which contains location name, latitude and longitude.
I tried using For loops which seems a little lame already but,
for(int i=0;i<=[myArray count];i++){
self.view = mapView_;
NSString *lat = [[myArray objectAtIndex:i] objectForKey:#"latitude"];
NSString *lon = [[myArray objectAtIndex:i] objectForKey:#"longitude"];
double lt=[lat doubleValue];
double ln=[lon doubleValue];
NSString *name = [[myArray objectAtIndex:i] objectForKey:#"name"];
NSLog(#"%# and %# and %f and %f of %#",lat,lon, lt,ln,name);
GMSMarker *marker = [[GMSMarker alloc] init];
marker.animated=YES;
marker.position = CLLocationCoordinate2DMake(lt,ln);
marker.title = name;
marker.snippet = #"Kathmandu";
marker.map = mapView_;
}
Here myarray is the array that has location name , latitude longitude in string format which I converted it to double. When I run this code Xcode shows me NSRangeException: index beyond bounds, which is probably because I am trying to use same object to display different indexes in same map. But at the same time, I couldnot think of any way to use GMSMarker as array.
I could however plot multiple markers if I used different GMSMarker objects, but that doesnot solve my problem. I made another object like this, using two GMSMarker objects work to show two markers on the same map.
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.animated=YES;
marker1.position = CLLocationCoordinate2DMake(lt,ln);
marker1.title = name;
marker1.snippet = #"Kathmandu";
marker1.map = mapView_;
Any help?
But at the same time, I couldnot think of any way to use GMSMarker as array.
try this:
NSMutableArray *markersArray = [[NSMutableArray alloc] init];
for(int i=0;i<[myArray count];i++){
// ... initialise marker here
marker.map = mapView_;
[markersArray addObject:marker];
[marker release];
}

Resources