Take a look at this code and see if you can help me fixing it:
- (void)viewDidLoad
{
//Initialize the array and add annotations for the location on the map
annotations = [[NSMutableArray alloc] init];
CustomAnnotation *annotation;
CLLocationCoordinate2D coordinate;
//1
coordinate.latitude = 25.220390105797264;
coordinate.longitude = 55.28095199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"123";
[annotations addObject:annotation];
//2
coordinate.latitude = 25.218880105797264;
coordinate.longitude = 55.27992199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"117";
[annotations addObject:annotation];
//3
coordinate.latitude = 25.219280105797264;
coordinate.longitude = 55.28052199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"118";
[annotations addObject:annotation];
//4
coordinate.latitude = 25.219680105797264;
coordinate.longitude = 55.28052199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"119";
[annotations addObject:annotation];
//5
coordinate.latitude = 25.219980105797264;
coordinate.longitude = 55.28095199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"120";
[annotations addObject:annotation];
//6
coordinate.latitude = 25.220380105797264;
coordinate.longitude = 55.28115199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"121";
[annotations addObject:annotation];
//7
coordinate.latitude = 25.220350105797264;
coordinate.longitude = 55.28105199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"122";
[annotations addObject:annotation];
//8
coordinate.latitude = 25.215050105797264;
coordinate.longitude = 55.28105199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"124";
[annotations addObject:annotation];
//9
coordinate.latitude = -35.44138;
coordinate.longitude = -71.66208;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"125";
[annotations addObject:annotation];
//10
coordinate.latitude = -35.44138;
coordinate.longitude = -71.66208;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"126";
[annotations addObject:annotation];
//Set the region and zoom factor of the map view
MKCoordinateRegion region;
region.span.latitudeDelta = 0.01f;
region.span.longitudeDelta = 0.01f;
region.center.latitude = 25.216050105797264;
region.center.longitude = 55.27905199798584;
[sheikhZayedRoadMapView addAnnotations:annotations];
[sheikhZayedRoadMapView setRegion:region];
//[siteMapView.layer setCornerRadius:5.0];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:
(id <MKAnnotation>)annotation
{
NSLog(#"Annotation Class: %#", [annotation class]);
static NSString * const kPinAnnotationIdentifier = #"PinIdentifier";
MKAnnotationView *annotationView;
Myriadproregular *annotationIndexLabel;
//CustomAnnotation *customAnnotation =nil
annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
if (!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kPinAnnotationIdentifier];
annotationIndexLabel = [[Myriadproregular alloc] initWithFrame:CGRectMake(0, 3, 25 , 20)];
annotationIndexLabel.font = [UIFont boldSystemFontOfSize:12];
[annotationIndexLabel setBackgroundColor:[UIColor clearColor]];
[annotationIndexLabel setTextColor:[UIColor whiteColor]];
[annotationIndexLabel setTextAlignment:UITextAlignmentCenter];
[annotationView addSubview:annotationIndexLabel];
annotationView.canShowCallout = YES;
annotationView.calloutOffset = CGPointMake(-5, 5);
}else {
//Get the label if exists
for (UIView *labelView in annotationView.subviews) {
if ([labelView isKindOfClass:[UILabel class]]) {
annotationIndexLabel = (Myriadproregular *) labelView;
break;
}
}
}
if ([annotation isKindOfClass:[MKUserLocation class]]) {
[annotationView setImage:[UIImage imageNamed:#"map_notify_bg.png"]];
}else {
CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation;
[annotationIndexLabel setText:customAnnotation.annotationIndex];
[annotationView setImage:[UIImage imageNamed:#"ico_inspector.png"]];
}
return annotationView;
}
Problem is:
I am not getting didSelectAnnotationView delegate method called, when I tap on the custom image provided to MKAnnotationView.
Any kind of help will be appreciated.
Thanks
[annotationView setCanShowCallout:NO]
This solved my problem, because I didn't want a callout on click of a pin, but I wanted to call the didSelectAnnotationView delegate to be called.
Thanks
Are you giving your annotations a title property? Are you sure that [annotationView setCanShowCallout:YES] is set properly?
Related
I am using mapview and want to add custom image to show the location in map view , how to add image i am not able to add. this code i have used.
-(void)addAllPins
{
self.mapView.delegate=self;
for(int i = 0; i < name1.count; i++)
{
[self addPinWithTitle:name1[i] AndCoordinate:arrCoordinateStr[i]];
}
}
-(void)addPinWithTitle:(NSString *)title AndCoordinate:(NSString *)strCoordinate
{
MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];
// clear out any white space
strCoordinate = [strCoordinate stringByReplacingOccurrencesOfString:#" " withString:#""];
// convert string into actual latitude and longitude values
NSArray *components = [strCoordinate componentsSeparatedByString:#","];
double latitude = [components[0] doubleValue];
double longitude = [components[1] doubleValue];
// setup the map pin with all data and add to map view
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
mapPin.title = title;
mapPin.coordinate = coordinate;
// UIImage *image = [UIImage imageNamed:#"hover.9.png"];
// [[self.mapView viewForAnnotation:mapPin] setImage:image];
[self.mapView addAnnotation:mapPin];
}
i want to set zoom in scale also . can some help me to solve this.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
// NSLog(#"%#", [self deviceLocation]);
//View Area
MKCoordinateRegion region = self.mapView.region;
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.015;
region.span.longitudeDelta = 0.015;
[mapView setRegion:region animated:YES];
[self addAllPins];
}
My code is help you.You can put custom pin annotation
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation
{
static NSString *reuseId = #"StandardPin";
MKAnnotationView *aView = (MKAnnotationView *)[sender
dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (aView == nil)
{
aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
}
aView.image = [UIImage imageNamed : #"Location_OnMap"];
aView.annotation = annotation;
aView.calloutOffset = CGPointMake(0, -5);
aView.draggable = YES;
aView.enabled = YES;
return aView;
}
You need to use mapView Delegate Method
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"MKPinAnnotationView"];
if (pinView ==nil) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"MKPinAnnotationView"];
pinView.animatesDrop = YES;
}
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorGreen;//if you want
return pinView;
}
else
{
static NSString *viewId = #"MKAnnotationView";
MKAnnotationView *annotationView = (MKAnnotationView*)
[mapView dequeueReusableAnnotationViewWithIdentifier:viewId];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:viewId];
}
annotationView.canShowCallout=YES;
annotationView.image = [UIImage imageNamed:#"yourImage"];//set your image here
return annotationView;
}
}
2.) to zoom
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(yourLocation.coordinate, 100, 100);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
It will help.Thank you.
He is desperately waiting for the solution... I am displaying the current user location and some custom annotations, but, when I am trying to zoom in into the map my map automatically redirecting to the current location with default zoom that is because of the span.please check my code and correct me.(i can able to zoom in other annotations when current location not displaying both simulator and device )
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
[self loadMultipleAnnotations];
self.map_view.delegate = self;
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D coordinate;
coordinate.latitude = newLocation.coordinate.latitude;
coordinate.longitude = newLocation.coordinate.longitude;
MKCoordinateSpan span;
span.latitudeDelta = 5;
span.longitudeDelta = 5;
MKCoordinateRegion region;
region.center = coordinate;
region.span= span;
[self.map_view setRegion:region];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate = coordinate;
[self.map_view setRegion:region animated:YES];
double radius = 40000.0;
MKCircle *circle1 = [MKCircle circleWithCenterCoordinate:coordinate radius:radius];
circle1.title = #"Current location Marking";
[self.map_view addOverlay:circle1];
}
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKCircle class]])
{
MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:(MKCircle*)overlay];
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.0];
circleView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
circleView.lineWidth = 3;
return circleView;
}
return nil;
}
-(void)loadMultipleAnnotations
{
NSMutableArray *arrLatti = [NSMutableArray arrayWithObjects:#"27.175015",#"28.171391",#"29.169005",#"22.105999",#"17.811456",#"21.453069",#"22.593726",#"38.444985",#"35.603719",#"35.603719",#"36.844461",#"35.889050",#"33.651208",#"38.238180",#"36.862043",#"36.949892",#"37.142803",#"37.71859", nil];
NSMutableArray *arrLong = [NSMutableArray arrayWithObjects:#"78.042155",#"79.037090",#"80.043206",#"75.761719",#"79.804688",#"81.562500",#"79.277344",#"-121.871338",#"-118.641357",#"-120.536499",#"-120.591431",#"-116.334229",#"-116.971436",#"-121.827393",#"-117.784424",#"-119.564209",#"-118.289795",#"-122.299805", nil];
NSMutableArray *arrTitle = [NSMutableArray arrayWithObjects:#"relative1",#"criminal1",#"criminal2",#"criminal3",#"criminal4",#"criminal5",#"criminal6",#"criminal7",#"criminal8",#"criminal9",#"relative2",#"client",#"criminal10",#"relative3",#"criminal11",#"relative4",#"criminal13",#"Offender", nil];
NSMutableArray *arrList = [[NSMutableArray alloc]init];
for (int i= 0; i< [arrLatti count]; i++)
{
List *list = [[List alloc]init];
list.latitt = [arrLatti objectAtIndex:i];
list.log = [arrLong objectAtIndex:i];
list.title = [arrTitle objectAtIndex:i];
[arrList addObject:list];
List *obj = [arrList objectAtIndex:i];
CLLocationCoordinate2D locationCoordinate;
locationCoordinate.latitude = [obj.latitt floatValue];
locationCoordinate.longitude = [obj.log floatValue];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate =locationCoordinate;
pointAnnotation.title = obj.title;
[self.map_view addAnnotation:pointAnnotation];
MKCoordinateSpan span;
span.latitudeDelta = 10;
span.longitudeDelta = 10;
MKCoordinateRegion region;
region.center = locationCoordinate;
region.span = span;
[self.map_view selectAnnotation:pointAnnotation animated:YES];
[self.map_view setRegion:region animated:YES];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *reuseId = #"reuseid";
MKAnnotationView *av = [_map_view dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] ;
}
else
{
av.annotation = annotation;
}
if ([annotation.title isEqualToString:#"criminal1"] || [annotation.title isEqualToString:#"Offender"] || [annotation.title isEqualToString:#"criminal2"]|| [annotation.title isEqualToString:#"criminal3"]|| [annotation.title isEqualToString:#"criminal4"]|| [annotation.title isEqualToString:#"criminal5"]|| [annotation.title isEqualToString:#"criminal6"]|| [annotation.title isEqualToString:#"criminal7"]|| [annotation.title isEqualToString:#"criminal8"]|| [annotation.title isEqualToString:#"criminal9"]|| [annotation.title isEqualToString:#"criminal10"]|| [annotation.title isEqualToString:#"criminal11"]|| [annotation.title isEqualToString:#"criminal12"]|| [annotation.title isEqualToString:#"criminal13"]|| [annotation.title isEqualToString:#"criminal14"]|| [annotation.title isEqualToString:#"criminal15"])
{
av.image = [UIImage imageNamed:#"marker.png"];
av.centerOffset = CGPointMake(0.0f, -16.5f);
}
else
{
av.image = [UIImage imageNamed:#"markerBlue.png"];
av.centerOffset = CGPointMake(0.0f, -24.5f);
}
av.canShowCallout = YES;
return av;
}
The trouble is this line in your -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation function. Every time iOS gets a new location it will set the map view to look at it...
[self.map_view setRegion:region animated:YES];
…and add another circle without removing the previous one. Why don't you just set self.map_view.showsuserLocation = true? It won't move the map view, but it will draw a nice blue dot and a circle where ever the device is on the map.
through below code i over come my problem .. now its looks me i have solved my issue
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if ( !location )
{
CLLocationCoordinate2D locationCoordinate;
locationCoordinate.latitude = userLocation.coordinate.latitude;
locationCoordinate.longitude = userLocation.coordinate.longitude;
location = userLocation.location;
MKCoordinateRegion region;
region.center = mapView.userLocation.coordinate;
region.span = MKCoordinateSpanMake(0.1, 0.1);
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate =locationCoordinate;
[self.map_View setCenterCoordinate:locationCoordinate animated:YES];
}
}
I have mapView that suppose to contain both user location pin and object pin. What i want, is show screen that contain both pins, no matter how large distance is between them (it suppose to be not too much). For some reason i can't show user location, screen moves to some point in the ocean. There is my code and attempts to get it:
-(MKCoordinateRegion)regionForAnnotations:(NSArray*)annotations{
MKCoordinateRegion region;
if ([annotations count] ==0){
region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 1000, 1000);
} else if ([annotations count] ==1 ){
id <MKAnnotation> annotation = [annotations lastObject];
region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000);
} else {
CLLocationCoordinate2D topLeftCord;
topLeftCord.latitude = -90;
topLeftCord.longitude = 180;
CLLocationCoordinate2D bottomRightCord;
bottomRightCord.latitude = 90;
bottomRightCord.longitude = -180;
for (id <MKAnnotation> annotation in annotations){
topLeftCord.latitude = fmax(topLeftCord.latitude, annotation.coordinate.latitude);
topLeftCord.longitude = fmin(topLeftCord.longitude, annotation.coordinate.longitude);
bottomRightCord.latitude = fmin(bottomRightCord.latitude, annotation.coordinate.latitude);
bottomRightCord.longitude = fmax(bottomRightCord.longitude, annotation.coordinate.longitude);
}
const double extraSpace = 1.1;
region.center.longitude = topLeftCord.longitude - (topLeftCord.longitude - bottomRightCord.longitude)/2.0;
region.center.latitude = topLeftCord.latitude - (topLeftCord.latitude - bottomRightCord.latitude)/2.0;
region.span.latitudeDelta = fabs(topLeftCord.latitude - bottomRightCord.latitude)*extraSpace;
region.span.longitudeDelta = fabs(topLeftCord.longitude - bottomRightCord.longitude)*extraSpace;
}
return [self.mapView regionThatFits:region];
}
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
//2
static NSString *identifier = #"Location";
MKPinAnnotationView *annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil){
annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
}
//3
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.pinColor = MKPinAnnotationColorPurple;
//4
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(getRoute:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
};
-(void)setEntityCoordiate{
CLLocationCoordinate2D location;
location.latitude = [self.latitudeString doubleValue];
location.longitude = [self.longitudeString doubleValue];
CLLocationCoordinate2D user;
user.latitude = self.mapView.userLocation.coordinate.latitude;
user.latitude = self.mapView.userLocation.coordinate.longitude;
CLLocation *userLocation = [[CLLocation alloc]initWithLatitude:user.latitude longitude:user.longitude ];
CLLocation *entityLocation = [[CLLocation alloc]initWithLatitude:location.latitude longitude:location.longitude];
MKCoordinateSpan span;
span.latitudeDelta = 100;
span.longitudeDelta = 100;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 400, 400);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = location;
annotation.title = _titleForEntity;
[_locations addObject:userLocation];
[_locations addObject:entityLocation];
[_mapView addAnnotation:annotation];
}
-(void)getRoute:(UIButton*)button{
MKCoordinateRegion region = [self regionForAnnotations:_locations];
[self.mapView setRegion:region animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setEntityCoordiate];
}
There is might be "too much" code, but i can't figure out where i made mistake i paste it all. I think i might have problem with array containing location objects.
How to finally make screen with that two locations? Any advice would be appreciated, thanks!
Try creating an area flyTo that will "contain" your given annotations:
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo)) {
flyTo = pointRect;
} else {
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
mapview.visibleMapRect = flyTo;
How do I add a button to my placemarks and then get it to push onto a view controller?
- (void)viewDidLoad
{
[super viewDidLoad];
// Set "More" logo in navigation bar
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Navigation"]];
appDelegate=[[UIApplication sharedApplication] delegate];
// Set longatude and latitude to tyne and wear
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(54.995184, -1.566699);
// Set span to cover area
MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
// Set region
MKCoordinateRegion regionToDisplay = MKCoordinateRegionMake(center, span);
[self.nearbyMapView setRegion: regionToDisplay];
for (int i = 0; i < [[appDelegate offersFeeds] count]; i++)
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
NSString *plotAddress = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:#"addressline"];
NSString *plotTitle = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:#"title"];
[geocoder geocodeAddressString:plotAddress completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks && placemarks.count > 0)
{
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];
// Set title
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = placemark.location.coordinate;
pa.title = plotTitle;
// Add placemark to map
[self.nearbyMapView addAnnotation:pa];
}
}];
}
}
I've had a look at MKAnotationView but struggling to understand how to get this working with CLPlacemark.
Add this before you add mapview to self.view
-(void)viewDidLoad
{
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = yourCoordinate;
annotation.title = #"Title";
annotation.subtitle = #"SubTitle";
[mapView1 addAnnotation:annotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
static NSString *defaultPinID = #"identifier";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
annotationView.enabled = YES;
pinView.canShowCallout = YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Accessoryview for the annotation view in ios.
pinView.rightCalloutAccessoryView = btn;
}
else
{
pinView.annotation = annotation;
}
pinView.annotation = annotation;
return pinView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
//Put your button stuff here...
}
I hope it is useful to you
I created a map view in my app. Now I want the user to be able to add several addresses to drop pins at those locations. I also want the user to be able to remove these pins.
Does anyone know where I can find a good tutorial, or where to start? I have no experience with mapviews...
try this......
-
(void)ShowPins
{
activity.hidden=YES;
[activity stopAnimating];
double lat;
double lng;
for (int ijk=0; ijk<arrayLocationList.count; ijk++)
{
/*Set your lat and long here*/
lat=[[[[arrayLocationList objectAtIndex:ijk]objectForKey:#"location"] objectForKey:#"lat"] doubleValue];
lng=[[[[arrayLocationList objectAtIndex:ijk]objectForKey:#"location"] objectForKey:#"lng"] doubleValue];
CLLocationCoordinate2D geos = CLLocationCoordinate2DMake(lat, lng);
MKPlacemark* marker = [[MKPlacemark alloc] initWithCoordinate:geos addressDictionary:nil];
[mapVieww addAnnotation:marker];
}
CLLocationCoordinate2D coord1 = {.latitude = lat, .longitude =lng};
MKCoordinateSpan span = {.latitudeDelta = .03,.longitudeDelta = .03};
MKCoordinateRegion region = {coord1, span};
[mapVieww setRegion:region];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
{
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"annotation1"];
UILabel *lable=[[UILabel alloc]init];
[newAnnotation addSubview:lable];
newAnnotation.pinColor = MKPinAnnotationColorRed;
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout = NO;
[newAnnotation setSelected:YES animated:YES];
return newAnnotation;
}
To Drop pins at several locations u have to try this code:
You need to put all those location latitude and longitude in an array and call this function everytime after incrementing the num value
-(void)Mapview
{ NSMutableArray* annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = [latit[num]doubleValue];
theCoordinate1.longitude = [longit[num]doubleValue];
Annotation* myAnnotation1=[[Annotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
[map_view addAnnotation:myAnnotation1];
map_view.showsUserLocation = NO;
[annotations addObject:myAnnotation1];
MKMapRect rect = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 20);
if (MKMapRectIsNull(rect)) {
rect = pointRect;
} else {
rect = MKMapRectUnion(rect, pointRect);
}
}
map_view.visibleMapRect = rect;
}
You can edit this code by adding leftcallout acessoryview or modify the contents in callout view .
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
map_view.showsUserLocation= NO;
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"AnnotationIdentifier"];
if(pinView == nil)
{
pinView = pinView=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:#"currentloc"] ;
pinView.centerOffset = CGPointMake(0,60);
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
}
return pinView;
}
To Remove the pins you can use this code
[self.map_view removeAnnotations:map_view.annotations];
(or)
[self.map_view removeAnnotations: [self.map_view.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"!(self isKindOfClass: %#)", [MKUserLocation class]]]];