I am trying to change colour of annotation pin on MKMapView by overriding this below:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"CustomPinAnnotationView"];
if (!pinView)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorGreen;
} else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
Here below are relevant methods:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
// display map ????
PFGeoPoint *geoPoint = self.detailItem[#"location"];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(geoPoint.latitude,geoPoint.longitude);
MKPointAnnotation *annotation =[[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
// remove previous annotation pin
if([self.mapView.annotations count] == 1) {
[self.mapView removeAnnotation:[self.mapView.annotations objectAtIndex:0]];
}
[self.mapView addAnnotation:annotation];
annotation.title = self.detailItem[#"text"];
MKCoordinateSpan span = {.latitudeDelta = 0.05, .longitudeDelta = 0.05};
MKCoordinateRegion region = {coordinate, span};
[self.mapView setRegion:region];
[self.mapView setCenterCoordinate:self.mapView.region.center animated:NO];
}
}
My problem is pin annotation colour is still shown Red as default not Green as I want. What am I missing here?
Credit to #Anna, add the line below to - (void)configureView
self.mapView.delegate = self;
Related
I am getting below debug info, and map is not loading at all.
Trying to initialize GEOVectorTile (2047.2047.12 GEOTileSetStyle_VECTOR_ROADS, GEOTileSize_PX512, GEOTileScale_NODPI) with non-VMP4 data.
below is the Code to Zoom to your Current Location.
mpView.showsUserLocation = YES;
[mpView setCenterCoordinate:mpView.userLocation.location.coordinate animated:YES];
[mpView showAnnotations:mpView.annotations animated:YES];
Here i have Created one Button, on click on button i can easily navigate to current location.
-(void)btnCurrentlocationClicked:(id)sender
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(ApplicationDelegate.locationManager.location.coordinate, 250, 250);
[mpView setRegion:region animated:YES];
[mpView setCenterCoordinate:mpView.userLocation.location.coordinate animated:YES];
// [mpView selectAnnotation:mapPin animated:YES];
[mpView showAnnotations:mpView.annotations animated:YES];
}
Here is my method for VierForAnnotation
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"viewForAnnotation");
if ([annotation isKindOfClass:[MKUserLocation class]]) {
NSLog(#"Is the user %f, %f", [annotation coordinate].latitude, [annotation coordinate].longitude);
// MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(ApplicationDelegate.locationManager.location.coordinate, 250, 250);
// [mpView setRegion:region animated:YES];
return nil;
}
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKAnnotationView *annotationView = [mpView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
NSString *strAnnotationSubtitle = [(MKPointAnnotation *)annotation subtitle];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: AnnotationIdentifier] ;
}
else
{
annotationView.annotation = annotation;
}
annotationView.draggable=YES;
annotationView.canShowCallout = YES;
if([strAnnotationSubtitle isEqualToString:#"Pickup"])
{
annotationView.image = [UIImage imageNamed:#"ic_pin_pickup"];
}
else if ([strAnnotationSubtitle isEqualToString:#"Drop"])
{
annotationView.image = [UIImage imageNamed:#"ic_pin_drop"];
}
return annotationView;
}
//You can customise as per your requirement.
i am using multiple annotation views with different images in my map view. Images are loading properly in simulator but not in device.i am not able to figure out the reason for this issue..following is my code
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString * const annotationIdentifier = #"CustomAnnotation";
annotationView1 = [mapUrCommView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
MKAnnotationView *annotationView2 = nil;
if (annotationView1)
{
annotationView1.annotation = annotation;
if(mapUrCommView.userInteractionEnabled)
annotationView1.image = [UIImage imageNamed:#"MapPinDarkBlue75#2x.png"];// map-pin-black.png
else
{
//if(annotationView1.annotation == self.currentAnnotation|| annotation == self.previousAnnotation)
NSLog(#"in annotation view 1");
annotationView1.image = [UIImage imageNamed:#"mapDrawPoint#2x.png"];// Bluedot.png
}
annotationView1.tag=111;
return annotationView1;
}
else
{
annotationView2 = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
annotationView2.image = [UIImage imageNamed:#"MapPinDarkBlue75#2x.png"];// map-pin-black.png
annotationView2.canShowCallout = YES;
return annotationView2;
}
return nil;
}
here i am drawing physical boundaries for a location in my app...so annotation view 1 is view which i am drawing presently & annoation view 2 will be having all annotations custom view images(all annotations will be having same images) which i already drawn in past..custom annotaion images are loading fine in simulator but not in device
It's worked me for using custom class for annotation.. Please refer this sample Apple documentation.
1.) Allocate MapAnnotation
mapAnnotation = [[MapAnnotation alloc]init];
2.) Set Coordinate for your lat and long
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[NBCGPSLocation sharedInstance].gpsLatitude doubleValue], [[NBCGPSLocation sharedInstance].gpsLongitude doubleValue]);
[mapAnnotation setCoordinate:coord];
[self.mapSDKView addAnnotation:mapAnnotation];
3.) Now this delegate method will call once you go that lat and long location
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[CustomAnnotation class]])
{
if(!customAnnotationView)
customAnnotationView = [[MKAnnotationView alloc]init];
customAnnotationView.image = [UIImage imageNamed:#"locationCustom"];
customAnnotationView.annotation = annotation;
return customAnnotationView;
}
else
{
if(!annotationView)
annotationView = [[MKAnnotationView alloc]init];
annotationView.image = [UIImage imageNamed:#"locationIcon"];
annotationView.annotation = annotation;
return annotationView;
}
}
I have multiple Annotation in a MapView, listed in 3 different arrays.
I've used the - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation method to change te Callout of the annotation.
The weird thing is that my UserLocation is changing into an Customised Annotation.
Why is that, and what could be the problem?
How I listed my Annotations:
myAnn = [[Annotations alloc]init];
location.latitude = 52.338847;
location.longitude = 4.937482;
myAnn.coordinate = location;
myAnn.title = #"Afvalpunt";
myAnn.subtitle = #"Rozenburglaan 1";
[category3 addObject:myAnn];
[self.locationArrays addObject:category3];
self.currentAnnotation = 0;
[self.myMapView addAnnotations:[self.locationArrays objectAtIndex:0]];
How my Method is set up:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"MapAn"];
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"MapAn"];
annotationView.canShowCallout = YES;
//Blauw Navigatie Auto...
UIImageView *carView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Driving"]];
UIButton *blueView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44+30)];
blueView.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
carView.frame = CGRectMake(11, 14, carView.image.size.width, carView.image.size.height);
[blueView addTarget:self action:#selector(carClicked) forControlEvents:UIControlEventTouchUpInside];
[blueView addSubview:carView];
annotationView.leftCalloutAccessoryView = blueView;
}
return annotationView;
}
Try this code.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if (annotation == mapView.userLocation) return nil;
...
We return nil if the annotation is userLocation to let the mapView display the blue dot & circle animation. In order to show our custom annotation for userLocation just remove the line return nil; and do your customization there.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* AnnotationIdentifier = #"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
if (annotation == mapView.userLocation)
{
customPinView.image = [UIImage imageNamed:#"myCarImage.png"];
}
else
{
customPinView.image = [UIImage imageNamed:#"mySomeOtherImage.png"];
customPinView.animatesDrop = NO;
customPinView.canShowCallout = YES;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
Hope this code is useful for you.
Try this code
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]] || annotation==mapView.userLocation)
{
return nil;//Here you can also customise your pin if you dont want pin then just return nil.
}
else
{
//your code
}
}
On initial load, the annotations show just fine. But if I scroll the map, they all disappear and the code is only called for the user location, not the other annotations in the viewForAnnotation delegate method.
Plot Pins
-(void)viewDidLoad{
...Download Coordinates and Data from Web here...
[self.mapView addAnnotation:pin];
}
Delegate Method
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
//Player's Pin
if([annotation class] == MKUserLocation.class) {
return nil;
}
//Cluster Pin
if([annotation isKindOfClass:[REVClusterPin class]]){
REVClusterPin *pin = (REVClusterPin *)annotation;
if( [pin nodeCount] > 0 ){
pin.title = #"___";
MKAnnotationView *annotationView = (REVClusterAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"cluster"];
if( !annotationView ){
annotationView = (REVClusterAnnotationView*)
[[REVClusterAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"cluster"];
}
annotationView.image = [UIImage imageNamed:#"cluster.png"];
[(REVClusterAnnotationView*)annotationView setClusterText:
[NSString stringWithFormat:#"%i",[pin nodeCount]]];
annotationView.canShowCallout = NO;
return annotationView;
}
}
//Player Pin
if([annotation isKindOfClass:[ZEPointAnnotation class]]){
ZEPointAnnotation *pin = (ZEPointAnnotation *)annotation;
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"pin"];
if(!annotationView){
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
}
annotationView.canShowCallout = YES;
annotationView.draggable = NO;
...Create Pin Data Here...
return annotationView;
}
return nil;
}
remove animations. that will solve your problem
I have the annotation ready to go, but trying to figure out on how to make it draggable with my code:
-(IBAction) updateLocation:(id)sender{
MKCoordinateRegion newRegion;
newRegion.center.latitude = mapView.userLocation.location.coordinate.latitude;
newRegion.center.longitude = mapView.userLocation.location.coordinate.longitude;
newRegion.span.latitudeDelta = 0.0004f;
newRegion.span.longitudeDelta = 0.0004f;
[mapView setRegion: newRegion animated: YES];
CLLocationCoordinate2D coordinate;
coordinate.latitude = mapView.userLocation.location.coordinate.latitude;
coordinate.longitude = mapView.userLocation.location.coordinate.longitude;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate: coordinate];
[annotation setTitle: #"Your Car is parked here"];
[annotation setSubtitle: #"Come here for pepsi"];
[mapView addAnnotation: annotation];
[mapView setZoomEnabled: YES];
[mapView setScrollEnabled: YES];
}
Thanks in advance!
To make an annotation draggable, set the annotation view's draggable property to YES.
This is normally done in the viewForAnnotation delegate method.
For example:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *reuseId = #"pin";
MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (pav == nil)
{
pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
pav.draggable = YES;
pav.canShowCallout = YES;
}
else
{
pav.annotation = annotation;
}
return pav;
}
If you need to handle when the user stops dragging and drops the annotation, see:
how to manage drag and drop for MKAnnotationView on IOS?
In addition, your annotation object (the one that implements MKAnnotation) should have a settable coordinate property. You are using the MKPointAnnotation class which does implement setCoordinate so that part's already taken care of.