This Blog describes how to show multiple locations in map view.
First you have to create an array in which multiple annotations will be saved.
In your ViewController.h file
NSMutableArray *addAnnotationsArray;
AddressAnnotation *addAnnotation;
In your ViewController.m file
addAnnotationsArray=[[NSMutableArray alloc]init];
Now initialize the for loop and and play it till how much locations you have to show on the map(In my case I have passed the count of my mapAddressArray which have the names of the locations ).
int k;
for(k=0;k<[self.mapAddressArray count];k++)
{
CLLocationCoordinate2D location;
I have called addressLocation method which returns the location coordinates of that location.
location=[self addressLocation:[self.mapAddressArray objectAtIndex:k]];
I have created seperate AddressAnnotation class and in this view controller created a object of that class and called initWithCoordinate method.
addAnnotation=[[AddressAnnotation alloc]initWithCoordinate:location];
[addAnnotation setMTitle:[self.companiesName objectAtIndex:k]];
[addAnnotation setMSubTitle:[self.mapAddressArray objectAtIndex:k]];
Add annotation to the addAnnotationsArray .
[self.addAnnotationsArray addObject:addAnnotation];
[addAnnotation release];
}
You have added the annotations in the array .Now you have to display these annotations on the map
int l;
for(l=0;l<[self.addAnnotationsArray count];l++)
{
[map addAnnotation:[self.mapAddAnnotations objectAtIndex:l]];
}
AddressLocation Method:-
-(CLLocationCoordinate2D) addressLocation:(NSString *)addrss {
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[addrss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
AddressAnnotation.h
#import<MapKit/MapKit.h>
@interface AddressAnnotation :NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *mTitle;
NSString *mSubTitle;
}
@property (nonatomic, retain) NSString *mSubTitle;
@property (nonatomic, retain) NSString *mTitle;
@end
AddressAnnotation.m
#import "AddressAnnotation.h"
@implementation AddressAnnotation
@synthesize coordinate,mTitle, mSubTitle,mapId;
- (NSString *)subtitle{
return mSubTitle;
}
- (NSString *)title{
return mTitle;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
//NSLog(@"%f,%f",c.latitude,c.longitude);
return self;
}
@end
First you have to create an array in which multiple annotations will be saved.
In your ViewController.h file
NSMutableArray *addAnnotationsArray;
AddressAnnotation *addAnnotation;
In your ViewController.m file
addAnnotationsArray=[[NSMutableArray alloc]init];
Now initialize the for loop and and play it till how much locations you have to show on the map(In my case I have passed the count of my mapAddressArray which have the names of the locations ).
int k;
for(k=0;k<[self.mapAddressArray count];k++)
{
CLLocationCoordinate2D location;
I have called addressLocation method which returns the location coordinates of that location.
location=[self addressLocation:[self.mapAddressArray objectAtIndex:k]];
I have created seperate AddressAnnotation class and in this view controller created a object of that class and called initWithCoordinate method.
addAnnotation=[[AddressAnnotation alloc]initWithCoordinate:location];
[addAnnotation setMTitle:[self.companiesName objectAtIndex:k]];
[addAnnotation setMSubTitle:[self.mapAddressArray objectAtIndex:k]];
Add annotation to the addAnnotationsArray .
[self.addAnnotationsArray addObject:addAnnotation];
[addAnnotation release];
}
You have added the annotations in the array .Now you have to display these annotations on the map
int l;
for(l=0;l<[self.addAnnotationsArray count];l++)
{
[map addAnnotation:[self.mapAddAnnotations objectAtIndex:l]];
}
AddressLocation Method:-
-(CLLocationCoordinate2D) addressLocation:(NSString *)addrss {
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[addrss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
AddressAnnotation.h
#import<MapKit/MapKit.h>
@interface AddressAnnotation :NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *mTitle;
NSString *mSubTitle;
}
@property (nonatomic, retain) NSString *mSubTitle;
@property (nonatomic, retain) NSString *mTitle;
@end
AddressAnnotation.m
#import "AddressAnnotation.h"
@implementation AddressAnnotation
@synthesize coordinate,mTitle, mSubTitle,mapId;
- (NSString *)subtitle{
return mSubTitle;
}
- (NSString *)title{
return mTitle;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
//NSLog(@"%f,%f",c.latitude,c.longitude);
return self;
}
@end
No comments:
Post a Comment