Skip to content

Commit

Permalink
Added a sample to add update marker and polyline
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth-kushwaha committed Sep 7, 2023
1 parent b20e647 commit 3c5aefa
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 0 deletions.
12 changes: 12 additions & 0 deletions MapplsSDKDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
18F45358251A81BB0091A898 /* DemoStyledAutocompleteViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18F45357251A81BB0091A898 /* DemoStyledAutocompleteViewController.swift */; };
18F576D023FEA83B0009A2D9 /* TextInstructions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18F576CF23FEA83B0009A2D9 /* TextInstructions.swift */; };
18F576D223FEAA880009A2D9 /* TextInstructions.plist in Resources */ = {isa = PBXBuildFile; fileRef = 18F576D123FEAA880009A2D9 /* TextInstructions.plist */; };
4692D7322AA8822800B907AB /* MovingMarkerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4692D7312AA8822800B907AB /* MovingMarkerViewController.swift */; };
4726F0562647C017007EDB5E /* ColorPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4726F0522647C017007EDB5E /* ColorPicker.swift */; };
4726F0582647C017007EDB5E /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4726F0542647C017007EDB5E /* SettingsViewController.swift */; };
4726F0592647C017007EDB5E /* ColorPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4726F0552647C017007EDB5E /* ColorPickerViewController.swift */; };
Expand Down Expand Up @@ -172,6 +173,7 @@
18F45357251A81BB0091A898 /* DemoStyledAutocompleteViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoStyledAutocompleteViewController.swift; sourceTree = "<group>"; };
18F576CF23FEA83B0009A2D9 /* TextInstructions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextInstructions.swift; sourceTree = "<group>"; };
18F576D123FEAA880009A2D9 /* TextInstructions.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = TextInstructions.plist; sourceTree = "<group>"; };
4692D7312AA8822800B907AB /* MovingMarkerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MovingMarkerViewController.swift; sourceTree = "<group>"; };
4726F0522647C017007EDB5E /* ColorPicker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPicker.swift; sourceTree = "<group>"; };
4726F0542647C017007EDB5E /* SettingsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
4726F0552647C017007EDB5E /* ColorPickerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPickerViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -338,6 +340,14 @@
path = InstructionFormatterHelpers;
sourceTree = "<group>";
};
4692D7302AA8820400B907AB /* Moving Marker */ = {
isa = PBXGroup;
children = (
4692D7312AA8822800B907AB /* MovingMarkerViewController.swift */,
);
path = "Moving Marker";
sourceTree = "<group>";
};
4726F0412647BCF3007EDB5E /* GeofenceUI */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -474,6 +484,7 @@
68417CC3210B2DCE00424A00 /* Swift */ = {
isa = PBXGroup;
children = (
4692D7302AA8820400B907AB /* Moving Marker */,
D1F1999D2A028ED900836308 /* PredictiveDirections */,
47858FE928A266B6003059A1 /* RasterCatalogue */,
47858FE628A26409003059A1 /* ReverseGeocode */,
Expand Down Expand Up @@ -872,6 +883,7 @@
18404BD92303D44D001D8DC0 /* ClusterMarkersExample.swift in Sources */,
68A6BF192106E03C00547929 /* ViewController.swift in Sources */,
A4DA1E9523DB16BF0010743F /* DirectionIndicationView.swift in Sources */,
4692D7322AA8822800B907AB /* MovingMarkerViewController.swift in Sources */,
18BA17E3229E8FF000B0B135 /* GreatCircle.swift in Sources */,
18D14C9D251A39B5004A6D32 /* AutosuggestWidgetExamplesLauncherVC.swift in Sources */,
47513EAF29FA402E00BA6C83 /* SetSymbolSortKeySample.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions MapplsSDKDemo/Swift/ListVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ class ListVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
let mapplsPinMarkerVC = CustomMapplsPinMarkerViewController()
mapplsPinMarkerVC.title = sampleType.title
self.navigationController?.pushViewController(mapplsPinMarkerVC, animated: false)
case .movingMarker:
let mapplsPinMarkerVC = MovingMarkerViewController()
mapplsPinMarkerVC.title = sampleType.title
self.navigationController?.pushViewController(mapplsPinMarkerVC, animated: false)
default:
let vctrl = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "mapVC") as? mapVC
self.navigationController?.pushViewController(vctrl!, animated: true)
Expand Down
201 changes: 201 additions & 0 deletions MapplsSDKDemo/Swift/Moving Marker/MovingMarkerViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
//
// MovingMarkerViewController.swift
// MapplsSDKDemo
//
// Created by CE00124321 on 06/09/23.
// Copyright © 2023 MMI. All rights reserved.
//

import UIKit
import MapplsMap
import MapplsAPICore
import MapplsDirectionUI

class MovingMarkerViewController: UIViewController {

var mapView: MapplsMapView!

var index = 0
var previousCoordinates: CLLocationCoordinate2D?

var locations: [CLLocationCoordinate2D] = []

let movingMarkerSourceIdentifier = "movingMarkerSourceIdentifier"
let movingMarkerLayerIdentifier = "movingMarkerLayerIdentifier"

let lineSourceIdentifier = "lineSourceIdentifier"
let lineLayerIdentifier = "lineLayerIdentifier"

override func loadView() {
super.loadView()
setUpMapView()
applyConstraint()
}

override func viewDidLoad() {
super.viewDidLoad()
}

func setUpMapView() {
mapView = MapplsMapView()
mapView.translatesAutoresizingMaskIntoConstraints = false
mapView.delegate = self
view.addSubview(mapView)
}

func applyConstraint() {
mapView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
}

}

//MARK: - MapView Delegates
extension MovingMarkerViewController: MapplsMapViewDelegate {

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
mapView.setCenter(CLLocationCoordinate2D(latitude: 28.55106676597232, longitude: 77.26892899885115), zoomLevel: 15, animated: true)

let currentWayPoint = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 28.551067906813493, longitude: 77.26890925366251), name: "Current Location")
let destinationWayPoint = Waypoint(coordinate: CLLocationCoordinate2D(latitude: 28.610231934703265, longitude: 77.22905502962782), name: "Destination")

let options = RouteOptions(waypoints: [currentWayPoint, destinationWayPoint])
Directions().calculate(options) { waypoints, routes, error in
if let coordinates = routes?.first?.coordinates {
self.locations = coordinates
self.startRoutePlay()
}
}
}

}

//MARK: - Start Tracking
extension MovingMarkerViewController {

@objc func startRoutePlay() {
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(startRoutePlay), object: nil)
addUpdateLayers()
perform(#selector(self.startRoutePlay), with: nil, afterDelay: TimeInterval(0.5))
index += 1
}

func addUpdateLayers() {
guard locations.indices.contains(index) else {
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(startRoutePlay), object: nil)
return
}
var heading = 0.0
let currentCoordinate = locations[index]

if let previousCoordinates = previousCoordinates {
heading = previousCoordinates.direction(to: currentCoordinate)
}
previousCoordinates = currentCoordinate

mapView.setCenter(currentCoordinate, zoomLevel: mapView.zoomLevel, direction: heading, animated: true)
addOrUpdateMovingMarkerLayer(coordinate: currentCoordinate, heading: heading)
addUpdateLineLayer(currentIndex: index)
}

}

//MARK: - Add Update Marker
extension MovingMarkerViewController {

private func addOrUpdateMovingMarkerLayer(coordinate: CLLocationCoordinate2D, heading: Double = 0) {
guard let style = mapView.style else { return }

var pointCollection = [MGLPointFeature]()

if coordinate.latitude > 0,
coordinate.longitude > 0,
CLLocationCoordinate2DIsValid(coordinate) {
let pointFeature = MGLPointFeature()
pointFeature.coordinate = coordinate
pointFeature.title = "realView-Annotation"
pointFeature.attributes = [
"type": "realViewAnnotation",
"icon-image": "Nearby-MapplsPin-Icon",
"icon-rotate": heading
]
pointCollection.append(pointFeature)
}

let pointCollectionShape = MGLShapeCollectionFeature(shapes: pointCollection)

if let source = style.source(withIdentifier: movingMarkerSourceIdentifier) as? MGLShapeSource {
source.shape = pointCollectionShape
} else {

let movingMarkerSource = MGLShapeSource(identifier: movingMarkerSourceIdentifier, shape: pointCollectionShape, options: nil)
style.addSource(movingMarkerSource)

let movingMarkerLayer = movingMarkerStyleLayer(identifier: movingMarkerLayerIdentifier, source: movingMarkerSource)
style.addLayer(movingMarkerLayer)
}

}

private func movingMarkerStyleLayer(identifier: String, source: MGLSource) -> MGLStyleLayer {
let symbolLayer = MGLSymbolStyleLayer(identifier: identifier, source: source)

if let style = mapView.style, style.image(forName: "Nearby-MapplsPin-Icon") == nil, let image = UIImage(named: "Vehicle") {
style.setImage(image, forName: "Nearby-MapplsPin-Icon")
}
symbolLayer.iconScale = NSExpression(forConstantValue: 0.7)
symbolLayer.sourceLayerIdentifier = "MovingMarkerLayer"
symbolLayer.iconImageName = NSExpression(forKeyPath: "icon-image")
symbolLayer.iconRotation = NSExpression(forKeyPath: "icon-rotate")
symbolLayer.iconRotationAlignment = NSExpression(forConstantValue: NSValue.init(mglIconRotationAlignment: .map))
symbolLayer.textFontNames = NSExpression(forConstantValue: ["Open Sans Regular"])
return symbolLayer
}

private func removeMovingMarkerLayer() {
guard let style = mapView.style else { return }

if let layer = style.layer(withIdentifier: movingMarkerLayerIdentifier) {
style.removeLayer(layer)
}
if let source = style.source(withIdentifier: movingMarkerSourceIdentifier) {
style.removeSource(source)
}
}

}

//MARK: - Add Update PolyLine
extension MovingMarkerViewController {

func addUpdateLineLayer(currentIndex: Int) {
guard let style = self.mapView.style else { return }

let sourceFeatures = getSourceFeatures(currentIndex: currentIndex)
// Create source and add it to the map style.
let source = MGLShapeSource(identifier: lineSourceIdentifier, shape: sourceFeatures, options: nil)

if let existingSource = style.source(withIdentifier: lineSourceIdentifier) as? MGLShapeSource {
existingSource.shape = sourceFeatures
} else {
style.addSource(source)

let polylineLayer = MGLLineStyleLayer(identifier: lineLayerIdentifier, source: source)
polylineLayer.lineColor = NSExpression(forConstantValue: UIColor.blue)
polylineLayer.lineOpacity = NSExpression(forConstantValue: 0.5)
polylineLayer.lineWidth = NSExpression(forConstantValue: 8)
style.addLayer(polylineLayer)
}

}

func getSourceFeatures(currentIndex: Int) -> MGLPolylineFeature? {
guard currentIndex < locations.count else { return nil }
var coordinates = Array(locations[currentIndex ..< locations.count])
let polyline = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count))
return polyline
}

}
3 changes: 3 additions & 0 deletions MapplsSDKDemo/Swift/Samples/SampleType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ enum SampleType: String, CaseIterable {
case directionUIPlugin
case nearbyUI
case setSymbolSortKeySample
case movingMarker

var title: String {
switch self {
Expand Down Expand Up @@ -174,6 +175,8 @@ enum SampleType: String, CaseIterable {
return "MapplsRasterCatalogue"
case .setSymbolSortKeySample:
return "Set Symbol Sort Key Sample"
case .movingMarker:
return "Moving Marker"
}
}
}

0 comments on commit 3c5aefa

Please sign in to comment.