Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Add annotation image replacement example #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
96D431FC1C84B4F7007D09D1 /* DrawingACustomMarkerExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 96D431FB1C84B4F7007D09D1 /* DrawingACustomMarkerExample.m */; };
96D432041C84B62A007D09D1 /* DrawingACustomMarkerExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96D432031C84B62A007D09D1 /* DrawingACustomMarkerExample.swift */; };
96D432061C84B9CF007D09D1 /* pisavector.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 96D432051C84B9CF007D09D1 /* pisavector.xcassets */; };
96D432091C851577007D09D1 /* ReplacingAMarkerImageExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96D432081C851577007D09D1 /* ReplacingAMarkerImageExample.swift */; };
96D4320C1C85161D007D09D1 /* ReplacingAMarkerImageExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 96D4320B1C85161D007D09D1 /* ReplacingAMarkerImageExample.m */; };
96D4320E1C8516CE007D09D1 /* markerreplacement.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 96D4320D1C8516CE007D09D1 /* markerreplacement.xcassets */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -139,6 +142,10 @@
96D431FB1C84B4F7007D09D1 /* DrawingACustomMarkerExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DrawingACustomMarkerExample.m; sourceTree = "<group>"; };
96D432031C84B62A007D09D1 /* DrawingACustomMarkerExample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DrawingACustomMarkerExample.swift; path = Swift/DrawingACustomMarkerExample.swift; sourceTree = "<group>"; };
96D432051C84B9CF007D09D1 /* pisavector.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = pisavector.xcassets; sourceTree = "<group>"; };
96D432081C851577007D09D1 /* ReplacingAMarkerImageExample.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ReplacingAMarkerImageExample.swift; path = Swift/ReplacingAMarkerImageExample.swift; sourceTree = "<group>"; };
96D4320A1C85161D007D09D1 /* ReplacingAMarkerImageExample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReplacingAMarkerImageExample.h; sourceTree = "<group>"; };
96D4320B1C85161D007D09D1 /* ReplacingAMarkerImageExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReplacingAMarkerImageExample.m; sourceTree = "<group>"; };
96D4320D1C8516CE007D09D1 /* markerreplacement.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = markerreplacement.xcassets; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -243,6 +250,7 @@
children = (
9682471C1C5C123B00494AB8 /* example.geojson */,
96D432051C84B9CF007D09D1 /* pisavector.xcassets */,
96D4320D1C8516CE007D09D1 /* markerreplacement.xcassets */,
);
path = Files;
sourceTree = "<group>";
Expand All @@ -260,6 +268,7 @@
9682470D1C5BF12700494AB8 /* DrawingAMarkerExample.h */,
9682471E1C5C1C0400494AB8 /* DrawingAPolygonExample.h */,
968247281C5C1FF800494AB8 /* PointConversionExample.h */,
96D4320A1C85161D007D09D1 /* ReplacingAMarkerImageExample.h */,
968247061C5BEBDC00494AB8 /* SatelliteStyleExample.h */,
9691AAA51C5AAD8F006A58C6 /* SimpleMapViewExample.h */,
);
Expand All @@ -280,6 +289,7 @@
9682470E1C5BF12700494AB8 /* DrawingAMarkerExample.m */,
9682471F1C5C1C0400494AB8 /* DrawingAPolygonExample.m */,
968247291C5C1FF800494AB8 /* PointConversionExample.m */,
96D4320B1C85161D007D09D1 /* ReplacingAMarkerImageExample.m */,
968247071C5BEBDC00494AB8 /* SatelliteStyleExample.m */,
9691AAA61C5AAD8F006A58C6 /* SimpleMapViewExample.m */,
);
Expand All @@ -299,6 +309,7 @@
9682470B1C5BF08400494AB8 /* DrawingAMarkerExample.swift */,
968247211C5C1C9B00494AB8 /* DrawingAPolygonExample.swift */,
9682472B1C5C20F800494AB8 /* PointConversionExample.swift */,
96D432081C851577007D09D1 /* ReplacingAMarkerImageExample.swift */,
968247091C5BEE4D00494AB8 /* SatelliteStyleExample.swift */,
9691AAAD1C5AB5A1006A58C6 /* SimpleMapViewExample.swift */,
);
Expand Down Expand Up @@ -414,6 +425,7 @@
9619629C1C581700002D3DAB /* Assets.xcassets in Resources */,
9682471D1C5C123B00494AB8 /* example.geojson in Resources */,
961962CF1C581FBC002D3DAB /* Main.storyboard in Resources */,
96D4320E1C8516CE007D09D1 /* markerreplacement.xcassets in Resources */,
96D432061C84B9CF007D09D1 /* pisavector.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -450,6 +462,7 @@
968247201C5C1C0400494AB8 /* DrawingAPolygonExample.m in Sources */,
9691AAAE1C5AB5A1006A58C6 /* SimpleMapViewExample.swift in Sources */,
96D431FC1C84B4F7007D09D1 /* DrawingACustomMarkerExample.m in Sources */,
96D4320C1C85161D007D09D1 /* ReplacingAMarkerImageExample.m in Sources */,
968247031C5BDCBB00494AB8 /* CustomRasterStyleExample.m in Sources */,
9682472A1C5C1FF800494AB8 /* PointConversionExample.m in Sources */,
96D432041C84B62A007D09D1 /* DrawingACustomMarkerExample.swift in Sources */,
Expand All @@ -467,6 +480,7 @@
968247151C5C102B00494AB8 /* CalloutDelegateUsageExample.swift in Sources */,
968247271C5C1DC700494AB8 /* CameraAnimationExample.m in Sources */,
961962D51C583FDC002D3DAB /* ExamplesContainerViewController.m in Sources */,
96D432091C851577007D09D1 /* ReplacingAMarkerImageExample.swift in Sources */,
9691AAB11C5AC5AD006A58C6 /* CustomStyleExample.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
1 change: 1 addition & 0 deletions Examples/Examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern NSString *const MBXExampleDrawingAPolygon;
extern NSString *const MBXExamplePointConversion;
extern NSString *const MBXExampleSatelliteStyle;
extern NSString *const MBXExampleSimpleMapView;
extern NSString *const MBXExampleReplacingAMarkerImage;

@interface Examples : NSObject

Expand Down
1 change: 1 addition & 0 deletions Examples/Examples.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ + (NSArray *)list {
MBXExampleSatelliteStyle,
MBXExamplePointConversion,
MBXExampleSimpleMapView,
MBXExampleReplacingAMarkerImage,
]];

NSMutableArray *meh = [[NSMutableArray alloc] init];
Expand Down
6 changes: 6 additions & 0 deletions Examples/Files/markerreplacement.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "apple.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "google.pdf"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Binary file not shown.
13 changes: 13 additions & 0 deletions Examples/ObjectiveC/ReplacingAMarkerImageExample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ReplacingAMarkerImageExample.h
// Examples
//
// Created by Jason Wray on 2/29/16.
// Copyright © 2016 Mapbox. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ReplacingAMarkerImageExample : UIViewController

@end
15 changes: 15 additions & 0 deletions Examples/ObjectiveC/ReplacingAMarkerImageExample.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// ReplacingAMarkerImageExample.m
// Examples
//
// Created by Jason Wray on 2/29/16.
// Copyright © 2016 Mapbox. All rights reserved.
//

#import "ReplacingAMarkerImageExample.h"

NSString *const MBXExampleReplacingAMarkerImage = @"ReplacingAMarkerImageExample";

@implementation ReplacingAMarkerImageExample

@end
2 changes: 1 addition & 1 deletion Examples/Swift/DrawingACustomMarkerExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DrawingACustomMarkerExample_Swift: UIViewController, MGLMapViewDelegate {
mapView = MGLMapView(frame: view.bounds, styleURL: MGLStyle.lightStyleURL())
mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

// Set the map's bounds to Pisa, Italy
// Set the maps bounds to Pisa, Italy
let bounds = MGLCoordinateBounds(sw: CLLocationCoordinate2D(latitude: 43.7115, longitude: 10.3725),
ne: CLLocationCoordinate2D(latitude: 43.7318, longitude: 10.4222))
mapView.setVisibleCoordinateBounds(bounds, animated: false)
Expand Down
77 changes: 77 additions & 0 deletions Examples/Swift/ReplacingAMarkerImageExample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// ReplacingAMarkerImageExample.swift
// Examples
//
// Created by Jason Wray on 2/29/16.
// Copyright © 2016 Mapbox. All rights reserved.
//

import Mapbox

@objc(ReplacingAMarkerImageExample_Swift)

class ReplacingAMarkerImageExample_Swift: UIViewController, MGLMapViewDelegate {

var mapView: MGLMapView!

override func viewDidLoad() {
super.viewDidLoad()

mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

view.addSubview(mapView)

// Set the map view‘s delegate property
mapView.delegate = self

addAnnotation()
}

func addAnnotation() {
let annotation = MGLPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(0, 0)
annotation.title = "MegaCorp Annotation"

mapView.addAnnotation(annotation)
}

func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
let reuseIdentifier = reuseIdentifierForAnnotation(annotation)
var annotationImage = mapView.dequeueReusableAnnotationImageWithIdentifier(reuseIdentifier)

if annotationImage == nil {
let image = UIImage(named: "apple")!
annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: reuseIdentifier)
}

return annotationImage
}

func mapView(mapView: MGLMapView, didSelectAnnotation annotation: MGLAnnotation) {
let reuseIdentifier = reuseIdentifierForAnnotation(annotation)

if let annotationImage = mapView.dequeueReusableAnnotationImageWithIdentifier(reuseIdentifier) {

if annotationImage.image == UIImage(named: "apple")! {
annotationImage.image = UIImage(named: "google")!
} else {
annotationImage.image = UIImage(named: "apple")!
}
}

mapView.deselectAnnotation(annotation, animated: false)
}

func reuseIdentifierForAnnotation(annotation: MGLAnnotation) -> String {
var reuseIdentifier = "\(annotation.coordinate.latitude),\(annotation.coordinate.longitude)"
if let title = annotation.title where title != nil {
reuseIdentifier += title!
}
if let subtitle = annotation.subtitle where subtitle != nil {
reuseIdentifier += subtitle!
}
return reuseIdentifier
}

}