Adding annotation on Map by parser latitude & longitude values - iphone

I have the following code:
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.0005;
span.longitudeDelta=0.0005;
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
for (int i = 0; i < [appDelegate.markers count]; i++) {
marker *aMarker = [appDelegate.markers objectAtIndex:i];
location.latitude = [[aMarker.lat objectAtIndex:i] floatValue];
location.longitude =[[aMarker.lng objectAtIndex:i] floatValue];
region.span=span;
region.center=location;
if(addAnnotation != nil)
{
[mapView removeAnnotation:addAnnotation];
[addAnnotation release];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation:addAnnotation];
}
I have parsed latitude and longitude in my XMLparser class. Now I want to add annotation on buttonclick event on Map. Can someone correct my code?

The warning NSString may not respond to -objectAtIndex means lat and lng are NSString objects (which don't have an objectAtIndex method).
You don't need to call objectAtIndex on lat and lng--they are the values in the specific marker object you just retrieved from the array.
(By the way, you get the warning when you compile the code--not when you "run" it like in your comment. Secondly, don't ignore compiler warnings. In this case, when you run the code you will get an exception: NSInvalidArgumentException - unrecognized selector.)
The other problem is the loop removes the previously added annotation before adding the current one. If you want to add all the markers to the map, this doesn't make sense. Currently, it would end up adding only the last marker to the map. If you really want to add only the last marker then you don't need to loop through the array (just grab the last marker from the array directly and create an annotation from it).
If instead you do want to add all the markers to the map, the loop should look like this:
for (int i = 0; i < [appDelegate.markers count]; i++)
{
marker *aMarker = [appDelegate.markers objectAtIndex:i];
location.latitude = [aMarker.lat floatValue];
location.longitude =[aMarker.lng floatValue];
AddressAnnotation *addrAnnot = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation:addrAnnot];
[addrAnnot release];
}
It's not clear what you're doing with region. If you're trying to set the region so that the map view shows all the annotations, look at the answers to this question: iOS MKMapView zoom to show all markers.

Related

crash application when scrolling map only when displaying annotaion

My application is crashing when I scrolling map with displaying one or more annotation. Without display annotation map is working fine.
Now I can't understand what is problem here. Please Help Me.
Following is code that I used to display All annotation.
-(void)displayAllAnnotation
{
NSLog(#"%d",[reminderTitle count]);
for (int i=0 ; i<[reminderTitle count]; i++)
{
double templati=[[reminderLatitude objectAtIndex:i]doubleValue];
double templongi=[[reminderLongitude objectAtIndex:i]doubleValue];
CLLocationCoordinate2D coord;
coord.latitude=(CLLocationDegrees)templati;
coord.longitude=(CLLocationDegrees)templongi;
DDAnnotation *ann = [[[DDAnnotation alloc] initWithCoordinate:coord addressDictionary:nil] autorelease];
ann.title = [reminderTitle objectAtIndex:i];
[self.mapView addAnnotation:ann];
[ann release];
}
}
You release ann object two times so your application is crashing.
remove autorelease. just write like this:
DDAnnotation *ann = [[DDAnnotation alloc] initWithCoordinate:coord addressDictionary:nil];

iPhone distance from current location as subtitle

I am trying to find distance from selectedAnnotation to userLocation. I added the following code in the annotation NSObject:
-(void) setDistanceFromCurrentLocation:(CLLocation *)currentLocation{
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:self.latitude longitude:self.longitude];
[self setDistance:[currentLocation distanceFromLocation:location2]];
}
- (NSString *)subtitle
{
NSString *myDistance = [NSString stringWithFormat:#"%1.1f from current location", distance];
return myDistance;
}
Now in the didUpdatedidUpdateToLocation I tried using the following logic from this question: https://stackoverflow.com/a/10881683/984248
Still getting 0.0 back. What am I doing wrong?
EDIT:
So I am calculating the distance from current location correctly now. But how do I pass this on to set it as the subtitle to a pin?
Here is how I am finding distance:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self setCurrentLocation:newLocation];
// if not Current Location then update the currently displayed Dealer Annotation
for (int i=0; i<self.dataArray.count; i++){
NSDictionary *dataDictionary = [self.dataArray objectAtIndex:i];
NSArray *array = [dataDictionary objectForKey:#"Locations"];
for (int i=0; i<array.count; i++){
NSMutableDictionary *dictionary = [array objectAtIndex:i];
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:[[dictionary objectForKey:#"Latitude"] doubleValue] longitude:[[dictionary objectForKey:#"Longitude"] doubleValue]];
[?????? setDistance:[self.currentLocation distanceFromLocation:pinLocation]];
}
}
}
Here is how I am adding pins to the map:
for (int i=0; i<self.dataArray.count; i++){
NSDictionary *dataDictionary = [self.dataArray objectAtIndex:i];
NSArray *array = [dataDictionary objectForKey:#"Locations"];
for (int i=0; i<array.count; i++){
NSMutableDictionary *dictionary = [array objectAtIndex:i];
MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.latitude = [[dictionary objectForKey:#"Latitude"] doubleValue];
annotation.longitude = [[dictionary objectForKey:#"Longitude"] doubleValue];
CLLocationCoordinate2D coord = {.latitude =
annotation.latitude, .longitude = annotation.longitude};
MKCoordinateRegion region = {coord};
annotation.title = [dictionary objectForKey:#"Name"];
annotation.subtitle = ?????;
annotation.coordinate = region.center;
//Saving the dictionary of the pin to show contact info later
annotation.sourceDictionary = dictionary;
[mapView addAnnotation:annotation];
}
}
Just wondering, is currentLocation set to self.latitude,self.longitude?
in which case you would be trying to find the distance from yourself. Try logging the latitude and longitude values of the currentLocation parameter and the self.latitude and self.longitude variables, and make sure they are different.
If they are, then try logging [currentLocation distanceFromLocation:location2] to see if it is non zero, if it is, then your setDistance method is the problem.
Other than that all I can think of is your distance variable is getting set to 0 somewhere else, or it is a variable that does not format with %1.1f so the formatter is setting it to 0.0

NSMutableArray seems to be prematurely releasing

I'm trying to add Annotations to an array to place multiple pins on a map. I have everything in a for loop. The first time it loops through, it adds the object to the array just fine. When it goes back through... the array has 0 objects in it. Can anyone tell me why?
EDIT: I'm using ARC.
-(void)plotMultipleLocs {
float latitude;
float longitude;
NSRange commaIndex;
NSString *coordGroups;
for (int i=0; i<=cgIdArray.count; i++) {
coordGroups = [cgInAreaArray objectAtIndex:i];
commaIndex = [coordGroups rangeOfString:#","];
latitude = [[coordGroups substringToIndex:commaIndex.location] floatValue];
longitude = [[coordGroups substringFromIndex:commaIndex.location + 1] floatValue];
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latitude, longitude);
MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000);
self->mapView.region = reg;
MKPointAnnotation* ann = [[MKPointAnnotation alloc] init];
ann.coordinate = loc;
ann.title = [cgNameArray objectAtIndex:i];
ann.subtitle = [cgLocArray objectAtIndex:i];
NSMutableArray *mutAnnArray = [NSMutableArray arrayWithArray:annArray];
[mutAnnArray addObject:ann];
}
}
You are creating a mutable array within the loop and adding your object to it.
At the next iteration of the loop, you create a new mutable array and add a new annotation to it.
Leave aside the fact that you are creating it from another array rather than just adding your annotation to annArray
Basically, the array that you are adding objects to last as long as one iteration, and then goes out of scope.
Try moving the array out of the loop:
-(void)plotMultipleLocs {
float latitude;
float longitude;
NSRange commaIndex;
NSString *coordGroups;
NSMutableArray *mutAnnArray = [NSMutableArray arrayWithArray:annArray]; // Create one array outside the loop.
for (int i=0; i<=cgIdArray.count; i++) {
coordGroups = [cgInAreaArray objectAtIndex:i];
commaIndex = [coordGroups rangeOfString:#","];
latitude = [[coordGroups substringToIndex:commaIndex.location] floatValue];
longitude = [[coordGroups substringFromIndex:commaIndex.location + 1] floatValue];
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latitude, longitude);
MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000);
self->mapView.region = reg;
MKPointAnnotation* ann = [[MKPointAnnotation alloc] init];
ann.coordinate = loc;
ann.title = [cgNameArray objectAtIndex:i];
ann.subtitle = [cgLocArray objectAtIndex:i];
[mutAnnArray addObject:ann]; // Add the annotation to the single array.
}
// mutAnnArray will go out of scope here, so maybe return it, or assign it to a property
}
Have you tried retaining the instance to avoid it being released?
Every time through the loop, you create a new mutable array with the contents of a different array. The mutable array containing the object you added on the previous iteration is not kept.

How to get a description for each pin, in Google Maps for the iPhone

I have a problem with MKPointAnnotation. I want to create my iPhone application like this:
Show a pin in Google Maps
Show a description on each pin, pulling that description from a NSMutableArray.
So, My question is, how do I show a description on each pin?
This is my code:
NSMutableArray *points = [[NSMutableArray alloc] init];
for(int i=0; i < [dataTable count]; i++) {
MKPointAnnotation *point =[[MKPointAnnotation alloc] init];
CLLocationCoordinate2D coordinate;
coordinate.latitude = [[[dataTable objectAtIndex:i] objectForKey:#"latitude"] floatValue];
coordinate.longitude = [[[dataTable objectAtIndex:i] objectForKey:#"longitude"] floatValue];
[point setCoordinate:coordinate];
[point setTitle:[[dataTable objectAtIndex:i] objectForKey:#"name"]]; //-- name of pin
[points addObject:point];
}
[map addAnnotations:points];
I would adopt the MKAnnotation protocol in my own class and simply override the
- (NSString) title
and implement
- (CLLocationCoordinate2D) coordinate {
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = self.latitude;
theCoordinate.longitude = self.longitude;
return theCoordinate;
}
My class would also have an initialiser that takes all the data it needs (from the array you mentioned)
- (id) initWithTitle:(NSString *)title_ andLatitude:(CLLocationDegrees)latitude_ andLongitude:(CLLocationDegrees)longitude_;
When iterating I would create my own objects and then add them to the collection of annotations.
Cheers...

How can i show Multiple pins on the map?

i want to show multiple pins on my MapView all with Animation of Dropping pin so how it is possible if any body have sample code then please send send link.i am new in this field.Thanks in Advance.
There are few code samples on developer.apple.com
This
is a simple map example with two pins
Just as you show single pin.. keep the code for single pin in Loop and pass different longitude latitude in loop.. You will get the pins at different location
if([points retainCount] > 0)
{
[points release];
points = nil;
}
if([annotationAry retainCount] > 0)
{
[annotationAry release];
annotationAry = nil;
}
points = [[NSMutableArray alloc]init];
annotationAry = [[NSMutableArray alloc]init];
for(int i=0;i<[longitudeary count];i++)
{
CLLocation* currentLocation1 = [[CLLocation alloc] initWithLatitude:[[latitudeary objectAtIndex:i]doubleValue] longitude:[[longitudeary objectAtIndex:i]doubleValue]];
[points addObject:currentLocation1];
}
for(int i=0;i<[points count];i++)
{
// CREATE THE ANNOTATIONS AND ADD THEM TO THE MAP
CSMapAnnotation* annotation = nil;
// create the start annotation and add it to the array
annotation = [[[CSMapAnnotation alloc] initWithCoordinate:[[points objectAtIndex:i] coordinate]
annotationType:CSMapAnnotationTypeImage
title:#"123456..."
shID:[shIDary objectAtIndex:i]
catID:[catIDary objectAtIndex:i]
ciggUse:[ciggaretteUSEary objectAtIndex:i]
wifiUse:[wifiUSEary objectAtIndex:i]
controller:self]autorelease];
[annotationAry addObject:annotation];
}
[mapViewmy addAnnotations:[NSArray arrayWithArray:annotationAry]];