Adding annotations from a KML file to an MKMapView - ios

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

Related

Attempting to drop pins based on MKMap from values from array

As the question says I am trying to add pins to my map based on the coordinates returned by my php file. Said file returns the following results
[{"dogid":"1","latitude":"15.435786","longitude":"-21.318447"},{"dogid":"1","latitude":"14.00000","longitude":"-18.536711"}]
What I am doing (well I believe i am) is taking the values from the link and saving them to a string. Secondly, save that string value to an array. Then, I go thru this array and save out the latitude and longitude and assign it to CLLocationCordinate 2dcoord. After whch I expect both pins to be dropped on whatever location they received.
However, what occurs is: Upon running the program, when it arrives on this lin
for (NSDictionary *row in locations) {
the loop is not run to assign the values, and it jumps to the end. Oddly, a single pin is dropped on the map (thou location doesnt appear to be the values that it waas passed).
Would appreciate a little incite into the matter.
Thanks
- (void)viewDidAppear:(BOOL)animated
{
NSMutableArray *annotations = [[NSMutableArray alloc] init];
NSURL *myURL =[NSURL URLWithString:#"link.php"];
NSError *error=nil;
NSString *str=[NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
CLLocationCoordinate2D coord;
NSArray *locations=[NSArray arrayWithContentsOfFile:str];
for (NSDictionary *row in locations) {
NSNumber *latitude = [row objectForKey:#"latitude"];
NSNumber *longitude = [row objectForKey:#"longitude"];
// NSString *title = [row objectForKey:#"title"];
//Create coordinates from the latitude and longitude values
coord.latitude = latitude.doubleValue;
coord.longitude = longitude.doubleValue;
}
MKPointAnnotation *pin = [[MKPointAnnotation alloc] init];
pin.coordinate = coord;
[self.mapView addAnnotation:pin];
}
It looks like you are trying to save api response to and Array.
Api always returns json string which is NSString.
You need to convert decode json string.
In your case
NSString *str=[NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
you need to decode str with [NSJSONSerialization JSONObjectWithData:<#(NSData )#> options:<#(NSJSONReadingOptions)#> error:<#(NSError *)#>] which give you proper array of dictionary.
Hope it will help you

filter map annotations by distance

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

plot marker on Google Maps iOS from JSON file

I would like to plot markers on Google Maps for iOS, and this by including JSON file that will includes longitude and latitude. I can do it manually in the code, by replacing the values.
The problem is that I don't know how to show new markers on the map from JSON file.
Here is my code :
- (void)addDefaultMarkers {
// Add a custom 'glow' marker around Sydney.
GMSMarker *sydneyMarker = [[GMSMarker alloc] init];
sydneyMarker.title = #"Sydney!";
sydneyMarker.icon = [UIImage imageNamed:#"glow-marker"];
sydneyMarker.position = CLLocationCoordinate2DMake(25.062718, 55.130761);
sydneyMarker.map = mapView_;
GMSMarker *melbourneMarker = [[GMSMarker alloc] init];
melbourneMarker.title = #"Melbourne!";
melbourneMarker.icon = [UIImage imageNamed:#"arrow"];
melbourneMarker.position = CLLocationCoordinate2DMake(25.100822, 55.17467);
melbourneMarker.map = mapView_;
}
Any ideas on how to do it ?
chech my question in this link, stackoverflow.com/questions/20902732/…. This link will help,
First of all parse the json data. And collect it as array or dictionary, then U can directly plot the value in map
The first two lines are to parse the data into an array
SBJsonParser *jsonParser = [SBJsonParser new];
NSArray *jsonData = (NSArray *) [jsonParser objectWithString:outputData error:nil];
then, the loop should continue till the no. of values,
for(int i=0;i<[jsonData count];i++)
{
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
Nslog(#"%#",dict);
double la=[[dict objectForKey:#"latitude"] doubleValue];
double lo=[[dict objectForKey:#"longitude"] doubleValue];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:la longitude:lo];
CLLocationCoordinate2D coordi=loca.coordinate;
marker=[GMSMarker markerWithPosition:coordi];
marker.snippet = #"Hello World";
marker.animated = YES;
marker.map = mapView;
}

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