Custom Annotation Showing Same Image for All Different Types of Poi'S

Image annotation different

I'm going to assume that you've updated Xcode so that you are now using Swift 3. Well then, the problem is that this method is never being executed:

func MapView(mapView: MKMapView!, 
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

... and the reason is that that is not its correct "signature". That method is now defined like this:

func mapView(_ mapView: MKMapView, 
viewFor annotation: MKAnnotation) -> MKAnnotationView?

How to use custom icons with mapKit framework?

You can change the annotation image by using following code,

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([[annotation title] isEqualToString:@"Current Location"]) {
return nil;
}

MKAnnotationView *annView = [[MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
if ([[annotation title] isEqualToString:@"McDonald's"])
annView.image = [ UIImage imageNamed:@"mcdonalds.png" ];
else if ([[annotation title] isEqualToString:@"Apple store"])
annView.image = [ UIImage imageNamed:@"applestore.png" ];
else
annView.image = [ UIImage imageNamed:@"marker.png" ];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:@selector(showDetailsView)
forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = infoButton;
annView.canShowCallout = YES;
return annView;
}

where "marker.png" is your image file.

How to add more than one color pin to a mapview.

Simple enough to do, just add an if statement to check the title or subtitle. When I was using maps, my annotation class had a subtitle where it would say what the place was, i.e. hotel, airport etc. I would just add an if statement to check, and then set what the pin point was and set up what colour I wanted the pin to be

if([annotation.subtitle isEqualToString:@"Hotel"])
{
pinView.animatesDrop=YES;
pinView.pinColor=MKPinAnnotationColorPurple;

}else if([annotation.subtitle isEqualToString:@"Airport"])
{
pinView.animatesDrop=YES;
pinView.pinColor=MKPinAnnotationColorRed;
}

or if you want it to be an image, just use

pinView.image = [UIImage imageNamed:@"airportMarker.png"];

EDIT: UPDATED CODE

Create a custom annotation class like this

@interface MyAnnotation : NSObject<MKAnnotation> 
{

CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
NSInteger categoryID;
}
@property NSInteger categoryID;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subtitle;

@end

Then when you are adding the objects to screen, give each one an ID, i.e

CLLocationCoordinate2D hotelCoordinates1;
hotelCoordinates1.latitude = 35.38355;
hotelCoordinates1.longitude = 1.11004;
MyAnnotation* exampleHotel=[[MyAnnotation alloc] init];
exampleHotel.coordinate=hotelCoordinates1;
exampleHotel.title=@"Example Hotel";
exampleHotel.subtitle=@"Hotel";
exampleHotel.categoryID = 1;

Then inside this method

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//create annotation

just do this

if([(MyAnnotation*)annotation categoryID] == 1)
{

}

You need to ensure you cast the annotation to whatever the name of the class you created. And thats it. Hope that helps!!

Trying to access the parent of an XSSFShape results in NullPointerException

This is because of incompleteness of apache poi until now (May 2021, version apache poi 5.0.0). It it affects all shapes in a shape group.

XSSFShape.getParent simply returns the class member XSSFShapeGroup parent of the XSSFShape. But while parsing the Office Open XML drawing, apache poi simply does the following:

...
} else if (obj instanceof CTGroupShape) {
shape = new XSSFShapeGroup(this, (CTGroupShape) obj);
}...

See https://svn.apache.org/viewvc/poi/tags/REL_5_0_0/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java?view=markup#l619.

And constructor of XSSFShapeGroup simply does

protected XSSFShapeGroup(XSSFDrawing drawing, CTGroupShape ctGroup) {
this.drawing = drawing;
this.ctGroup = ctGroup;
}

See https://svn.apache.org/viewvc/poi/tags/REL_5_0_0/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFShapeGroup.java?view=markup#l55.

So this lacks traversing all shapes in that group and set their parent to that shape group. So parent always will be null.

So either you get the parent using the low level classes, as you have shown already in your question. Or, better, you do checking the container in your method traverseShapeContainer(ShapeContainer<XSSFShape> container), which is the parent container of all traversed shapes. You can get whether this is a XSSFShapeGroup or is the XSSFDrawing.

Like so:

...
... void traverseShapeContainer(ShapeContainer<XSSFShape> container) {
for (XSSFShape shape : container) {
// possible: XSSFConnector, XSSFGraphicFrame, XSSFPicture, XSSFShapeGroup, XSSFSimpleShape
if (shape instanceof XSSFConnector) {
XSSFConnector connector = (XSSFConnector)shape;
System.out.println(connector);

} else if (shape instanceof XSSFGraphicFrame) {
XSSFGraphicFrame graphicFrame = (XSSFGraphicFrame)shape;
System.out.println(graphicFrame);

} else if (shape instanceof XSSFPicture) {
XSSFPicture picture = (XSSFPicture)shape;
System.out.println(picture);
if (container instanceof XSSFDrawing) {
System.out.println("Picture is in drawing directly.");
} else if (container instanceof XSSFShapeGroup) {
System.out.println("Picture is in shape group.");
XSSFShapeGroup parent = (XSSFShapeGroup) container;
System.out.println("Parent is " + parent);
}

} else if (shape instanceof XSSFShapeGroup) { //we have a shape group
XSSFShapeGroup shapeGroup = (XSSFShapeGroup)shape;
System.out.println(shapeGroup);

traverseShapeContainer(shapeGroup); // we now can sinply get the XSSFShapeGroup as ShapeContainer<XSSFShape>

} else if (shape instanceof XSSFSimpleShape) {
XSSFSimpleShape simpleShape = (XSSFSimpleShape)shape;
System.out.println(simpleShape);

}
}

}
...


Related Topics



Leave a reply



Submit