I have used MKMapView to show map and current user location which is working correctly.
Now I want to draw a polyline as user moves but it is not working I tried follwing code:
for(int i = 0; i < [longarray count]; i++)
{
NSNumber *latt=[latarray objectAtIndex:i];
NSNumber *lonn=[longarray objectAtIndex:i];
sklat =[[NSString stringWithFormat:#"%#",latt]doubleValue];
sklongi =[[NSString stringWithFormat:#"%#",lonn]doubleValue];
CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(sklat,sklongi);
// break the string down even further to latitude and longitude fields.
MKMapPoint point = MKMapPointForCoordinate(coordinate1);
// if it is the first point, just use them, since we have nothing to compare to yet.
pointsArray[i] = point;
}
self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:[latarray count]];
free(pointsArray);
[mapview addOverlay:self.routeLine];
then i usedd overlay function as
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine)
{
routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine] ;
routeLineView.fillColor = [UIColor colorWithRed:0.000 green:5.100 blue:0.100 alpha:1];
routeLineView.strokeColor = [UIColor colorWithRed:0.000 green:5.100 blue:0.100 alpha:1];
routeLineView.lineWidth = 4;
overlayView = routeLineView;
}
return overlayView;
}
Related
I am developing Iphone app and using Google map.Draw Multiple Polygon on google Map. but than deleting the polygon on map that deleting the last polygon not delete all draw polygon on google map.please help thanks in advance.
code..
// Draw Polygon
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
if (buttonClicked == true) {
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
_latLongDict = #{#"lat":#(coordinate.latitude), #"long":#(coordinate.longitude)};
[_clickCoordinate addObject:_latLongDict];
NSLog(#"%#",_clickCoordinate);
_path = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (NSDictionary *dic in _clickCoordinate) {
event.latitude = [[dic valueForKey:#"lat"] floatValue];
event.longitude = [[dic valueForKey:#"long"] floatValue];
[_path addCoordinate:event];
}
_polygun = [GMSPolygon polygonWithPath:_path];
_polygun.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
_polygun.strokeColor = [UIColor blackColor];
_polygun.strokeWidth = 2;
_polygun.map = mapView;
}
// Delete Polygon
- (IBAction)cancelButton:(id)sender {
for (int i = 0; i < _path.count; i++) {
_polygun.map = nil;
[_clickCoordinate removeAllObjects];
[_path removeAllCoordinates];
NSLog(#"%#",_clickCoordinate);
}
}
I have been facing problem which is mkoverlay color. When I open the mapview, sometimes instead of drawing walking path, it colors with biking activity. I do not know how to fix the problem. Even though I have not done any biking activity but it draws biking activity with blue color.
Here is the code implementation.
- (void)showLines {
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSArray* coordinate_array = [[NSArray alloc] init];
int arrayCount = 0;
// walking
NSData *data =[def objectForKey:#"walking_coordinate"];
NSMutableArray *walking_array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
coordinate_array = [NSArray arrayWithArray:walking_array];
arrayCount = (int)[walking_array count];
color = 1;
[self parseArray:coordinate_array withArrayCount:arrayCount];
// driving
data =[def objectForKey:#"driving_coordinate"];
NSMutableArray *driving_array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
coordinate_array = [NSArray arrayWithArray:driving_array];
arrayCount = (int)[driving_array count];
color = 2;
[self parseArray:coordinate_array withArrayCount:arrayCount];
// biking
data =[def objectForKey:#"biking_coordinate"];
NSMutableArray *biking_array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
coordinate_array = [NSArray arrayWithArray:biking_array];
arrayCount = (int)[biking_array count];
color = 3;
[self parseArray:coordinate_array withArrayCount:arrayCount];
}
- (void) parseArray:(NSArray *) coordinate_array withArrayCount:(int)arrayCount
{
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < arrayCount; i++) {
CoordinateModel *coord = [coordinate_array objectAtIndex:i];
[tempArray addObject:coord];
if ((int)coord.latitude == -1 || (int)coord.longitude == -1 || i == arrayCount-1) {
// this is end of one segment
[tempArray removeLastObject];
CLLocationCoordinate2D *pointsCoordinate = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * [tempArray count]);
for (int j = 0; j < [tempArray count]; j++) {
CoordinateModel *point = [tempArray objectAtIndex:j];
CLLocationCoordinate2D old_coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
pointsCoordinate[j] = old_coordinate;
// NSLog(#"(%f, %f)", old_coordinate.latitude, old_coordinate.longitude);
}
if ([tempArray count] > 0) {
int countTemp = (int)[tempArray count];
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:pointsCoordinate count:countTemp];
[mapView addOverlay:polyline];
[tempArray removeAllObjects];
}
}
}
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineView *lineView = [[MKPolylineView alloc] initWithPolyline:overlay];
lineView.lineWidth = 8;
if (color == 1) {
// walking
lineView.strokeColor = [UIColor greenColor];
lineView.fillColor = [UIColor greenColor];
}
else if(color == 2) {
// driving
lineView.strokeColor = [UIColor redColor];
lineView.fillColor = [UIColor redColor];
}
else if(color == 3) {
// biking
lineView.strokeColor = [UIColor blueColor];
lineView.fillColor = [UIColor blueColor];
}
else {
lineView.strokeColor = [UIColor blackColor];
lineView.fillColor = [UIColor blackColor];
}
return lineView;
}
return nil;
}
In the viewForOverlay delegate method, the overlay color is set using the external variable color which is set before calling parseArray for each type of overlay.
However, there is no guarantee when the delegate method will be called by the map view and it's possible for the delegate method to be called multiple times for the same overlay after you've already added all the overlays (eg. if you zoom/pan the map and the overlay comes back into view).
Since the last color value you set is 3 (for "biking"), any calls that the map view makes to the delegate method after the overlays are already added will end up drawing the overlay with the biking color.
To fix this, you need to be able to determine what color to draw the overlay inside the delegate method itself using some property of the overlay parameter (and not relying on some external variable).
The simplest way to do this is to use the MKPolyline's title property.
See this answer and this answer that explain the fact that MKPolyline has a title property.
So what you could do in your case is:
Make color a parameter that you pass to your parseArray method.
In the parseArray method, after creating polyline, set its title to the color:
polyline.title = [NSString stringWithFormat:#"%d", color];
In viewForOverlay, check the overlay's title property and set the color accordingly. See a specific example in different coloured polygon overlays (it shows it for polygon but the same can be done with polylines).
Here is the answer based on Anna answer to help whomever wants to see in code.
- (void)showLines2 {
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSArray* coordinate_array = [[NSArray alloc] init];
int arrayCount = 0;
// walking
NSData *data =[def objectForKey:#"walking_coordinate"];
NSMutableArray *walking_array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
coordinate_array = [NSArray arrayWithArray:walking_array];
arrayCount = (int)[walking_array count];
color = 1;
[self parseArray:coordinate_array withArrayCount:arrayCount withColor:color];
// driving
data =[def objectForKey:#"driving_coordinate"];
NSMutableArray *driving_array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
coordinate_array = [NSArray arrayWithArray:driving_array];
arrayCount = (int)[driving_array count];
color = 2;
[self parseArray:coordinate_array withArrayCount:arrayCount withColor:color];
// biking
data =[def objectForKey:#"biking_coordinate"];
NSMutableArray *biking_array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
coordinate_array = [NSArray arrayWithArray:biking_array];
arrayCount = (int)[biking_array count];
color = 3;
[self parseArray:coordinate_array withArrayCount:arrayCount withColor:color];
}
- (void) parseArray:(NSArray *) coordinate_array withArrayCount:(int)arrayCount withColor:(int)polyColor
{
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < arrayCount; i++) {
CoordinateModel *coord = [coordinate_array objectAtIndex:i];
[tempArray addObject:coord];
if ((int)coord.latitude == -1 || (int)coord.longitude == -1 || i == arrayCount-1) {
// this is end of one segment
[tempArray removeLastObject];
CLLocationCoordinate2D *pointsCoordinate = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * [tempArray count]);
for (int j = 0; j < [tempArray count]; j++) {
CoordinateModel *point = [tempArray objectAtIndex:j];
CLLocationCoordinate2D old_coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
pointsCoordinate[j] = old_coordinate;
// NSLog(#"(%f, %f)", old_coordinate.latitude, old_coordinate.longitude);
}
if ([tempArray count] > 0) {
int countTemp = (int)[tempArray count];
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:pointsCoordinate count:countTemp];
polyline.title = [NSString stringWithFormat:#"%d", color];
[mapView addOverlay:polyline];
[tempArray removeAllObjects];
}
}
}
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineView *lineView = [[MKPolylineView alloc] initWithPolyline:overlay];
lineView.lineWidth = 8;
// ActivityType currentActivityType = [DataManager sharedInstance].activityType;
if ([overlay.title isEqualToString:#"1"]) {
// walking
lineView.strokeColor = [UIColor greenColor];
lineView.fillColor = [UIColor greenColor];
}
else if([overlay.title isEqualToString:#"2"]) {
// driving
lineView.strokeColor = [UIColor redColor];
lineView.fillColor = [UIColor redColor];
}
else if([overlay.title isEqualToString:#"3"]) {
// biking
lineView.strokeColor = [UIColor blueColor];
lineView.fillColor = [UIColor blueColor];
}
else {
lineView.strokeColor = [UIColor blackColor];
lineView.fillColor = [UIColor blackColor];
}
return lineView;
}
return nil;
}
I have an application which shows something around 100 Annotations (with custom pin-images, callout accessories and annotation-images) in a MapView. While building the annotations I store a link between annotation and building so I can assign the right building and open the right segue afterwards.
In iOS 6 they get built really fast, I also enabled animation while adding them, so one pin got dropped after the other, but with apple maps in iOS7 this isn't possible anymore (?). Now building those 100 annotations takes over 1 second on my iPhone 4S and that's too long. Is there anyway to improve the code?
- (void)viewDidLoad
...
//creating annotations
annotationlink = [[NSMutableArray alloc] init];
for (int i = 0; i < data.count; i++) {
NSDictionary *dataItem = [data objectAtIndex:i];
//storing annotation in array for link
Annotation *buildingannotation = [[Annotation alloc] init];
NSNumber *index = [NSNumber numberWithInteger:i];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:index, indexKey, buildingannotation, annotationKey, nil];
[annotationlink addObject:dict];
buildingannotation.title = [dataItem objectForKey:#"Building"];
buildingannotation.subtitle = [dataItem objectForKey:#"Info"];
MKCoordinateRegion buildingcoordinates;
buildingcoordinates.center.latitude = [[dataItem objectForKey:#"Latitude"] floatValue];
buildingcoordinates.center.longitude = [[dataItem objectForKey:#"Longitude"] floatValue];
buildingannotation.coordinate = buildingcoordinates.center;
[self.mapView addAnnotation:buildingannotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
MKAnnotationView *pinView = (MKAnnotationView *)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];
MKAnnotationView *customAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
customAnnotationView.canShowCallout = YES;
//right button to detail view
UIButton* disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customAnnotationView.rightCalloutAccessoryView = disclosureButton;
//left button for image
NSInteger *buildingindex = [self getIndex:annotation];
NSDictionary *dataItem = [data objectAtIndex:buildingindex];
NSString* filename = [dataItem objectForKey:#"Thumb"];
filename = [filename stringByAppendingString:#"#2x.jpg"];
NSString* resourceimagePath = [resourcePath stringByAppendingPathComponent:filename];
Image = [UIImage imageWithContentsOfFile:resourceimagePath];
UIImageView *AnnotationThumb = [[UIImageView alloc] initWithImage:Image];
AnnotationThumb.frame = CGRectMake(0, 0, 31, 31);
customAnnotationView.leftCalloutAccessoryView = AnnotationThumb;
//annotation image
customAnnotationView.image = [UIImage imageNamed:#"Annotation_white.png"];
return customAnnotationView;
return pinView;
}
the following function gets the index of the current annotation using nspredicate to filter the array with the dictionaries. the advantage of this is the fact, that i can also use it when calloutAccessoryControlTapped:
-(NSInteger*) getIndex:(Annotation*)searchannotation
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K == %#", annotationKey, searchannotation];
NSArray *filteredarray = [annotationlink filteredArrayUsingPredicate:predicate];
NSDictionary *building = [filteredarray objectAtIndex:0];
NSInteger *buildingIndex = [[building objectForKey:indexKey] integerValue];
return buildingIndex;
}
With an iPhone 4S the last pin is built 1.14 seconds after the view gets loaded.
if i search the annotation link array manually instead of using nspredicate function like this:
//left button for image
int buildingIndex;
for (int i = 0; i < annotationlink.count; i++) {
NSDictionary *annotationDict = [annotationlink objectAtIndex:i];
if ([[annotationDict objectForKey:annotationKey] isEqual:annotation]) {
buildingIndex= [[annotationDict objectForKey:indexKey] integerValue];
i = annotationlink.count;
}
}
NSDictionary *dataItem = [data objectAtIndex:buildingIndex];
the log says that the last pin is built 1.89 seconds after the viewDidLoad.
if i create the annotations in viewDidApper instead of viewDidLoad the View is shown off course immediately but the background takes some time to load so until the pins are dropped everything is gray which is also not very nice...
Thank you Anna for your suggestions! I implemented the improvements like this:
Annotation.h:
#import <MapKit/MKAnnotation.h>
#interface Annotation : NSObject <MKAnnotation> {}
#property(nonatomic, assign) CLLocationCoordinate2D coordinate;
#property(nonatomic, copy) NSString *title;
#property(nonatomic, copy) NSString *subtitle;
#property NSInteger *ID;
#end
Annotation.m:
#import "Annotation.h"
#implementation Annotation
#synthesize coordinate, title, subtitle, ID;
#end
ViewDidAppear:
//creating annotations
for (int i = 0; i < data.count; i++) {
NSDictionary *dataItem = [data objectAtIndex:i];
Annotation *buildingannotation = [[Annotation alloc] init];
buildingannotation.ID = i;
buildingannotation.title = [dataItem objectForKey:#"Building"];
buildingannotation.subtitle = [dataItem objectForKey:#"Subtitle"];
MKCoordinateRegion buildingcoordinates;
buildingcoordinates.center.latitude = [[dataItem objectForKey:#"Latitude"] floatValue];
buildingcoordinates.center.longitude = [[dataItem objectForKey:#"Longitude"] floatValue];
buildingannotation.coordinate = buildingcoordinates.center;
[self.mapView addAnnotation:buildingannotation];
}
viewForAnnotation:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
MKAnnotationView *pinView = (MKAnnotationView *)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];
if (pinView == nil) {
MKAnnotationView *customAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
customAnnotationView.canShowCallout = YES;
//right button to detail view
UIButton* disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customAnnotationView.rightCalloutAccessoryView = disclosureButton;
//left button for image
Annotation *buildingAnnotation = (Annotation *)annotation;
NSInteger *buildingindex = buildingAnnotation.ID;
NSString *filePath = [thumbname objectAtIndex:buildingindex];
UIImageView *AnnotationThumb = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
AnnotationThumb.frame = CGRectMake(0, 0, 31, 31);
customAnnotationView.leftCalloutAccessoryView = AnnotationThumb;
//annotation image
customAnnotationView.image = [UIImage imageNamed:#"Annotation_white.png"];
return customAnnotationView;
} else {
pinView.annotation = annotation;
}
return pinView;
}
Callout:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
Annotation *buildingAnnotation = (Annotation *)view.annotation;
selectedbuilding = buildingAnnotation.ID;
[self performSegueWithIdentifier:#"DetailViewController" sender:self];
}
Takes still some time for showing all Annotations. Is there any chance to further improve the code?
I updated the vievForAnnotation function regarding to Anna's reply and the PhotosByLocation Sample Application. It works now and I hope it's the correct way to implement the reuse...
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
MKAnnotationView *buildingAnnotationView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];
if (buildingAnnotationView) {
[buildingAnnotationView prepareForReuse];
} else {
buildingAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
buildingAnnotationView.canShowCallout = YES;
//right button to detail view
UIButton* disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
buildingAnnotationView.rightCalloutAccessoryView = disclosureButton;
//annotation image
buildingAnnotationView.image = [UIImage imageNamed:#"Annotation_white.png"];
}
//left button for image
Annotation *buildingAnnotation = (Annotation *)annotation;
NSInteger *buildingindex = buildingAnnotation.ID;
NSString *filePath = [thumbname objectAtIndex:buildingindex];
UIImageView *AnnotationThumb = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
AnnotationThumb.frame = CGRectMake(0, 0, 31, 31);
buildingAnnotationView.leftCalloutAccessoryView = AnnotationThumb;
return buildingAnnotationView;
}
I have a problem when trying to add a polyline on the map.
I get directions from MKDirections, and create a RMPolylineAnnotation, but it does not appear on the map.
As I can see, the following method is called just once when the map appears:
- (RMMapLayer *)mapView:(RMMapView *)mapView layerForAnnotation:(RMAnnotation *)annotation
Here is the code I am using:
- (void)getRoute {
CLLocation *source = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocation *destination = [[CLLocation alloc] initWithLatitude:self.mapView.selectedAnnotation.coordinate.latitude longitude:self.mapView.selectedAnnotation.coordinate.longitude];
[AppleDirectionManager getDirectionsFromPoint:source toPoint:destination transportType:MKDirectionsTransportTypeAutomobile withCompletion:^(MKDirectionsResponse *result) {
self.routeStepsArray = [[result.routes firstObject] steps];
for (MKRoute *route in result.routes) {
NSMutableArray *points = [NSMutableArray array];
for (int i = 0; i < route.polyline.pointCount; i++) {
MKMapPoint point = route.polyline.points[i];
CLLocation *location = [[CLLocation alloc] initWithLatitude:point.x longitude:point.y];
[points addObject:location];
}
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *arr = [points copy];
RMPolylineAnnotation *polyline = [[RMPolylineAnnotation alloc] initWithMapView:self.mapView points:arr];
[self.mapView addAnnotation:polyline];
});
}
}];
}
- (RMMapLayer *)mapView:(RMMapView *)mapView layerForAnnotation:(RMAnnotation *)annotation {
if (annotation.isUserLocationAnnotation)
return nil;
RMMarker *marker = [[RMMarker alloc] initWithUIImage:[UIImage imageNamed:#"blabla.png"]];
[marker setCanShowCallout:YES];
[marker setName:#"blabla"];
return marker;
}
Also I can not show a custom callout for a RMPointAnnotation.
For RMPointAnnotation it's easily.
_polylineAnnotation = [[RMPolylineAnnotation alloc] initWithMapView:_mapView points:#[Location,Location]];
_polylineAnnotation.lineColor = [UIColor redColor];
_polylineAnnotation.lineWidth = 10.0f;
[_mapView addAnnotation:_polylineAnnotation];
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]]]];