I have a Boolean variables called points. The initial value of points is setted to false because I want a certain behaviour. Then in a function I want to change the value of the points variable to true.
I read some documentation about swift (because I'm new) but I'm not getting why the value of points is not changing.
import SwiftUI
import MapboxMaps
import MapboxCoreMaps
import UIKit
import CoreLocation
struct MapBoxView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> MapViewController {
return MapViewController()
}
func updateUIViewController(_ uiViewController: MapViewController, context: Context) {}
}
#objc(MapViewController)
public class MapViewController: UIViewController {
internal var mapView: MapView!
private let customImage = UIImage(named: "location-pin-1")!
var n: Int = 0
override public func viewDidLoad() {
super.viewDidLoad()
let centerCoordinate = CLLocationCoordinate2D(latitude: 50, longitude: 10)
let options = MapInitOptions(cameraOptions: CameraOptions(center: centerCoordinate, zoom: 2.4))
mapView = MapView(frame: view.bounds, mapInitOptions: options)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(mapView)
mapView.mapboxMap.onNext(.mapLoaded) { _ in
self.setupExample()
}
}
public func setupExample() {
addTapGesture(to: mapView)
}
public func addTapGesture(to mapView: MapView) {
self.n = self.n + 1
if (self.n < 2) {
let tapGesture = UILongPressGestureRecognizer(target: self, action: #selector(addPoint))
mapView.addGestureRecognizer(tapGesture)
}
}
#objc public func addPoint(_ sender: UITapGestureRecognizer) {
let tapPoint = sender.location(in: mapView)
let coordinate = mapView.mapboxMap.coordinate(for: tapPoint)
let pointAnnotationManager = mapView.annotations.makePointAnnotationManager()
var customPointAnnotation = PointAnnotation(coordinate: coordinate)
customPointAnnotation.image = .init(image: customImage, name: "my-custom-image-name")
pointAnnotationManager.annotations = [customPointAnnotation]
}
}
This all is included in a class that use UIViewController.
This is not SwiftUI, so remove the #State property wrapper, make it just var points: Bool = false
If you are using UITapGestureRecognizer and UIViewController then you are using UIKit not SwiftUI
Related
I am having a problem with the map within my app. I set the starting location as the center of the users location, but when I go to move the map around, it doesn't allow me to and/or moves automatically back to the center.
I have userTrackingMode set to .follow. I can't really think of anything else that might be causing this to happen, although I am fairly new to Xcode and Swift.
Here is where I think the problem occurs:
import Foundation
import SwiftUI
import MapKit
struct UberMapViewRepresentable: UIViewRepresentable {
let mapView = MKMapView()
let locationManager = LocationManager()
#EnvironmentObject var locationViewModel: LocationSearchViewModel
func makeUIView(context: Context) -> some UIView {
mapView.delegate = context.coordinator
mapView.isRotateEnabled = false
mapView.showsUserLocation = true
mapView.userTrackingMode = .follow
return mapView
}
func updateUIView(_ uiView: UIViewType, context: Context) {
if let coordinate = locationViewModel.selectedLocationCoordinate {
context.coordinator.addAndSelectAnnotation(withCoordinate: coordinate)
}
}
func makeCoordinator() -> MapCoordinator {
return MapCoordinator(parent: self)
}
}
extension UberMapViewRepresentable {
class MapCoordinator: NSObject, MKMapViewDelegate {
// MARK: - Properties
let parent: UberMapViewRepresentable
// MARK: - Lifecycle
init(parent: UberMapViewRepresentable) {
self.parent = parent
super.init()
}
// MARK: - MKMapViewDelegate
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
let region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude),
span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
)
parent.mapView.setRegion(region, animated: true)
}
// MARK: - Helpers
func addAndSelectAnnotation(withCoordinate coordinate: CLLocationCoordinate2D) {
parent.mapView.removeAnnotations(parent.mapView.annotations)
let anno = MKPointAnnotation()
anno.coordinate = coordinate
parent.mapView.addAnnotation(anno)
parent.mapView.selectAnnotation(anno, animated: true)
parent.mapView.showAnnotations(parent.mapView.annotations, animated: true)
}
}
}
I have a map which loads annotations from the Google API, when the map initially loads all the annotations they are 'placed' as seen through the print in the console, however they won't show up on the map until I move the map once. Does anyone know if I need to call a method to update the map after placing the annotations?
struct ContentView: View {
var locationSearch = LocationSearch()
#State private var mapView = MapView()
#State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -33.7944, longitude: 151.2649), span: MKCoordinateSpan(latitudeDelta: 0.015, longitudeDelta: 0.015))
#EnvironmentObject var sheetManager: SheetManager
var body: some View {
mapView
.popup(with: SheetManager())
.frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight)
}
}
struct MapView: UIViewRepresentable {
#State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -33.7944, longitude: 151.2649), span: MKCoordinateSpan(latitudeDelta: 0.015, longitudeDelta: 0.015))
func updateUIView(_ uiView: MKMapView, context: Context) {
print("FLF: MapView updated")
uiView.setNeedsDisplay()
}
var locationManager = CLLocationManager()
let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: UIScreen.screenHeight))
func setupManager() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
}
func makeUIView(context: Context) -> MKMapView {
setupManager()
mapView.region = ContentView().region
mapView.showsUserLocation = true
mapView.userTrackingMode = .follow
mapView.delegate = context.coordinator // set the delegate to the coordinator
placeMarkersForRegion(region: region)
return mapView
}
func placeMarkersForRegion(region: MKCoordinateRegion) {
var locationSearch = LocationSearch()
locationSearch.performSearch(region: region) { venues in
print("FLF: Placing \(venues.count) marker(s)")
for marker in venues {
let annotation = MKPointAnnotation()
annotation.coordinate = marker.location
annotation.title = marker.name
mapView.addAnnotation(annotation)
}
}
}
func makeCoordinator() -> MapViewCoordinator {
MapViewCoordinator(self) // pass self to the coordinator so it can call `regionDidChangeAnimated`
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// Use the 'coordinate' property to get the current location of the map view
let currentRegion = mapView.region
print("FLF: Map has moved")
self.placeMarkersForRegion(region: currentRegion)
// Do something with the current region (e.g. update a state variable or perform a search)
}
}
class MapViewCoordinator: NSObject, MKMapViewDelegate {
var parent: MapView // add a property to hold a reference to the parent view
init(_ parent: MapView) {
self.parent = parent
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// Call the parent's implementation of this method
parent.mapView(mapView, regionDidChangeAnimated: animated)
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
view.canShowCallout = true
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
// Get the tapped annotation
guard let annotation = view.annotation else { return }
// Print the title of the annotation
print(annotation.title ?? "Unknown")
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print("FLF: Marker tapped")
}
}
The UIViewRepresentable and Coordinator aren't implemented correctly. E.g. makeUIView has to init it, but you are initing it as a property on the struct which is immediately lost. Also MapViewCoordinator(self) is a mistake because self, i.e. the struct, is immediately disgarded after SwiftUI has updated.
Another issue is the #State shouldn't hold a View like how your ContentView has a #State for the MapView.
Here is an example of how to use MKMapView with UIViewRepresentable:
struct MKMapViewRepresentable: UIViewRepresentable {
#Binding var userTrackingMode: MapUserTrackingMode
func makeCoordinator() -> Coordinator {
Coordinator()
}
func makeUIView(context: Context) -> MKMapView {
context.coordinator.mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {
// MKMapView has a strange design that the delegate is called when setting manually so we need to prevent an infinite loop
context.coordinator.userTrackingModeChanged = nil
uiView.userTrackingMode = userTrackingMode == .follow ? MKUserTrackingMode.follow : MKUserTrackingMode.none
context.coordinator.userTrackingModeChanged = { mode in
userTrackingMode = mode == .follow ? MapUserTrackingMode.follow : MapUserTrackingMode.none
}
}
class Coordinator: NSObject, MKMapViewDelegate {
lazy var mapView: MKMapView = {
let mv = MKMapView()
mv.delegate = self
return mv
}()
var userTrackingModeChanged: ((MKUserTrackingMode) -> Void)?
func mapView(_ mapView: MKMapView, didChange mode: MKUserTrackingMode, animated: Bool) {
userTrackingModeChanged?(mode)
}
}
}
So I have a MapView which adds a pin to the current user location. Also I have two textfields which displays the coordinates of the pin. I want to add two features:
When I drag and drop the pin, I want to update the coordinates inside the textfields.
When I edit the coordinates in the textfield, it should move the pin to the updated coorinates from the textfields.
Here my code where i handle everything for MapView. Im still pretty new to coding, so the code could be confusing, sorry for that.
class LocateVC: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var finishedLocateButton: UIButton!
#IBOutlet weak var relocateButton: UIButton!
var coordinates: [[Double]]!
var latTitleLabel:[String]!
var latValueLabel:[String]!
var lngTitleLabel:[String]!
var lngValueLabel: [String]!
var isEdited = false
var customCallout: UIView?
//Important to track location
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
view.layer.backgroundColor = Colors.grey.cgColor
//button titles
relocateButton.isHidden = true
relocateButton.setTitle("Relocate", for: .normal)
finishedLocateButton.setTitle("Continue", for: .normal)
navigationItem.title = "Location"
// Ask for Authorisation from the User.
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
self.mapView.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
if isEdited == false {
if CLLocationManager.locationServicesEnabled() {
addPin()
}
}
// Latitude,Longitude
coordinates = [
[ProductData.shared.latitude!, ProductData.shared.longitude!],
]
} //end of viewDidLoad
override func viewWillAppear(_ animated: Bool) {
super.dismiss(animated: animated)
}
public func removePin() {
}
func dropPinFor(placemark: MKPlacemark) {
for annotation in mapView.annotations {
if annotation.isKind(of: MKPointAnnotation.self) {
// mapView.removeAnnotation(annotation) // removing the pins from the map
}
}
let annotation = MKPointAnnotation()
annotation.coordinate = placemark.coordinate
mapView.addAnnotation(annotation)
}
//1
public func addPin() {
if isEdited == false {
ProductData.shared.latitude = locationManager.location?.coordinate.latitude
ProductData.shared.longitude = locationManager.location?.coordinate.longitude
}
self.mapView.delegate = self
// adds an annotation to coordinates from productData
let point = ProductAnnotation(coordinate: CLLocationCoordinate2D(latitude: ProductData.shared.latitude! , longitude: ProductData.shared.longitude!))
self.mapView.addAnnotation(point)
// 3
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: ProductData.shared.latitude!, longitude: ProductData.shared.longitude!), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
self.mapView.setRegion(region, animated: true)
}
public func editCoord() {
performSegue(withIdentifier: "editCoord", sender: CustomCalloutView.self)
}
#IBAction func relocateButtonClicked(_ sender: Any) {
addPin()
}
#IBAction func finishedLocateButtonClicked(_ sender: Any) {
performSegue(withIdentifier: "finishedLocateSegue2", sender: self)
}
//4
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
if view.isKind(of: CustomCalloutView.self ) || view.isKind(of: AnnotationView.self) || view.isKind(of: ProductAnnotation.self) {
return
} else {
customCallout?.removeFromSuperview()
}
}
//3
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if view.annotation is MKUserLocation {
return
}
//this creates the callout
let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
let calloutView = views?[0] as! CustomCalloutView
calloutView.delegate = self
calloutView.lngTitleLabel.text = "Lng"
calloutView.latTitleLabel.text = "Lat"
calloutView.lngTextField.text = String(format:"%f", ProductData.shared.longitude!)
calloutView.latTextField.text = String(format:"%f", ProductData.shared.latitude!)
calloutView.latTextField.layer.borderWidth = 0.0
calloutView.lngTextField.layer.borderWidth = 0.0
calloutView.latTextField.isEnabled = false
calloutView.lngTextField.isEnabled = false
calloutView.latTextField.keyboardType = .numberPad
calloutView.lngTextField.keyboardType = .numberPad
calloutView.alpha = 1.0
calloutView.layer.cornerRadius = 8
calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
customCallout = calloutView
view.addSubview(calloutView)
mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}
//2
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
if annotationView == nil{
annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "Pin")
annotationView?.isDraggable = true
annotationView?.canShowCallout = false
} else {
annotationView?.annotation = annotation
}
annotationView?.image = UIImage(named: "dot")
return annotationView
}
func saveButtonTapped() {
customCallout?.removeFromSuperview()
}
}
extension LocateVC: CustomCalloutViewDelegate {
func didClickEditButton() {
editCoord()
}
func didClickSaveButton() {
saveButtonTapped()
}
}
and here my custom callout:
class CustomCalloutView: UIView {
#IBOutlet weak var latTitleLabel: UILabel!
#IBOutlet weak var lngTitleLabel: UILabel!
#IBOutlet weak var editButton: UIButton!
#IBOutlet weak var saveButton: UIButton!
#IBOutlet weak var latTextField: UITextField!
#IBOutlet weak var lngTextField: UITextField!
var delegate: CustomCalloutViewDelegate?
var isEditing: Bool = false
#IBAction func didClickEditButton(_ sender: Any) {
// delegate?.didClickEditButton()
isEditing = true
latTextField.layer.borderWidth = 1.0
lngTextField.layer.borderWidth = 1.0
latTextField.isEnabled = true
lngTextField.isEnabled = true
saveButton.isHidden = false
}
#IBAction func saveButtonTapped(_ sender: Any) {
if isEditing == true {
if let lat = latTextField.text {
ProductData.shared.latitude = Double(lat)
}
if let lng = lngTextField.text {
ProductData.shared.longitude = Double(lng)
}
latTextField.layer.borderWidth = 0.0
lngTextField.layer.borderWidth = 0.0
latTextField.isEnabled = false
lngTextField.isEnabled = false
self.saveButton.setTitle("Save", for: .normal)
isEditing = false
} else {
self.saveButton.setTitle("Cancel", for: .normal)
delegate?.didClickSaveButton()
}
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
if self.bounds.contains(point) {
return true
} else {
return self.subviews.contains { $0.frame.contains(point) }
}
}
}
protocol CustomCalloutViewDelegate {
func didClickEditButton()
func didClickSaveButton()
}
any idea how i can achive that? Maybe in a general way.
Thank you in advance
Here's a link to DSCenterPinMapView. A CocoaPod I recommend you to use.
It is a custom MapView with an animated and customizable center pin useful for selecting locations in map.
Here is also a guide on installing CocoaPods if it is new to you.
You should install the Pod an then, as a solution to your questions:
Implement the delegate so that you can get the location where pin drops.
pinMapView.delegate = self
extension MyViewController: DSCenterPinMapViewDelegate {
func didStartDragging() {
// My custom actions
}
func didEndDragging() {
// My custom actions
selectedLocation = pinMapView.mapview.centerCoordinate
// Update Text field
}
}
After you finish adding the coordinates on your TextFields you should call
pinMapView.center(on coordinate: myTextFieldCoordinate)
to center the map on the desired location.
After changing the marker position get coordinates like
let latitude = marker.coordinate.latitude
let longitude = marker.coordinate.longitude
And to set coordinate
let coordinate = CLLocationCoordinate2D(latitude: "your lat", longitude: "your long")
marker.coordinate = coordinate
mapView.setCenter(coordinate, animated: true)
if you not keeping the marker reference then
guard let marker = self.appleMap.annotations.first else {return}
let latitude = marker.coordinate.latitude
let longitude = marker.coordinate.longitude
And to set coordinate
guard let marker = self.appleMap.annotations.first else {return}
let coordinate = CLLocationCoordinate2D(latitude: "your lat", longitude: "your long")
marker.coordinate = coordinate
mapView.setCenter(coordinate, animated: true)
Hope that will help.
I can't figure out why the table view controller is not populating with placeMarks.name and coordinates. The user taps a pin on the map and adds the location to the model. Where then it sent to the table view controller, however, it is not displaying the data from the model. Below is my code.
import Foundation
import CoreLocation
class PlaceList : NSObject {
let locations: Places
var coordinate: CLLocationCoordinate2D { return locations.coordinate }
init(point: Places) {
self.locations = point
super.init()
}
var title: String? {
return locations.name
}
var subtitle: String? {
return "(\(locations.coordinate.latitude), \(locations.coordinate.longitude))"
}
}
import CoreLocation
import Foundation
class Places: NSObject {
var name: String
var coordinate: CLLocationCoordinate2D
init(name: String, coordinate: CLLocationCoordinate2D) {
self.name = name
self.coordinate = coordinate
}
}
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate,CLLocationManagerDelegate {
var placeModel: PlaceList?
var pointOfInterest: [Places] = []
var poi: [Places] = [] {
didSet {
pointOfInterest = poi
}
}
//Creating mapview object
var mapView: MKMapView!
var geoCoder = CLGeocoder()
//Initial cooridinate
var latitude = 43.136581
var longitude = -87.941101
var latitudeDelta = 0.3
var longitudeDelta = 0.3
//the object that determines the location
let placeManger = CLLocationManager()
//loading the map on the scene
override func loadView() {
super.loadView()
mapView = MKMapView()
view = mapView
placeManger.startUpdatingLocation()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
navigationItem.title = "Travel Wishlist"
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.barTintColor = .blue
placeManger.delegate = self
placeManger.requestWhenInUseAuthorization()
mapView.delegate = self
navigationItem.rightBarButtonItem = rightBar
navigationItem.leftBarButtonItem = leftBar
mapView.addGestureRecognizer(tapGestrueRecongnizer)
centerMapInInitialCoordinates()
showPointsOfInterestInMap()
}
func showPointsOfInterestInMap() {
mapView.removeAnnotations(mapView.annotations)
for point in poi {
let pin = PlaceList(point: point)
mapView.addAnnotation(pin as! MKAnnotation)
}
}
func centerMapInInitialCoordinates() {
let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta)
let region:MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), span: span)
mapView.setRegion(region, animated: true)
}
var tapGestrueRecongnizer: UITapGestureRecognizer {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(loadPointOfInterests(recongnizer:)))
return tapGesture
}
var rightBar: UIBarButtonItem {
let rightBarButton =
UIBarButtonItem(title: "Find a place to add",
style: .plain, target: self,
action: #selector(rightbarButton))
rightBarButton.tintColor = .white
return rightBarButton
}
var leftBar: UIBarButtonItem {
let leftBarButton =
UIBarButtonItem(title:
"Look up", style: .plain,
target: self, action: #selector(leftButton))
leftBarButton.tintColor = .white
return leftBarButton
}
#objc func leftButton(leftButton: UIBarButtonItem) {
print("Left BarButton")
performSegue(withIdentifier: "place", sender: leftButton)
addPlace()
}
#objc func rightbarButton(right: UIBarButtonItem) {
addPlace()
}
func addPlace(){
// mapView.removeAnnotations(mapView.annotations)
for point in pointOfInterest {
let pin = PlaceList(point: point)
mapView.addAnnotation(pin as! MKAnnotation)
}
filterVisiblePOI()
}
func filterVisiblePOI() {
let visibleAnnotations = self.mapView.annotations(in: self.mapView.visibleMapRect)
var annotations = [PlaceList]()
for visibleAnnotation in visibleAnnotations {
if let annotation = visibleAnnotation as? PlaceList {
annotations.append(annotation)
}
}
}
#objc func loadPointOfInterests(recongnizer:UITapGestureRecognizer) {
let locationPoint = recongnizer.location(in: mapView)
let cooridinate = mapView.convert(locationPoint, toCoordinateFrom: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = cooridinate
mapView.addAnnotation(annotation)
geoCoder.reverseGeocodeLocation(CLLocation(latitude: cooridinate.latitude, longitude: cooridinate.longitude)) {(placeMarks : [CLPlacemark]?, error: Error?) in
if let placeLocation = placeMarks?[0] {
if error == nil && (placeMarks?.count)! > 0 {
let locationString = " \(placeLocation.name!)"
annotation.coordinate = cooridinate
annotation.title = locationString
print(locationString)
}
for point in self.pointOfInterest {
let pin = PlaceList(point: point)
self.mapView.addAnnotation(pin as! MKAnnotation)
self.poi.append(point)
print(point)
print(self.pointOfInterest)
print(pin)
}
}
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
self.navigationItem.rightBarButtonItem?.title = "Find a place to add"
}
private func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
//To Do
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "place" {
let nav = segue.destination as! UINavigationController
let detailViewControll = nav.topViewController as! VisitationTableViewController
for point in pointOfInterest {
let pin = PlaceList(point: point)
mapView.addAnnotation(pin as! MKAnnotation)
detailViewControll.poi = pointOfInterest
}
}
}
}
import Foundation
import UIKit
class VisitationTableViewController: UITableViewController {
var poi: [Places] = []
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
let dissButton = UIBarButtonItem(title: "Done", style: .done
, target: self, action: #selector(dismisController))
self.navigationItem.rightBarButtonItem = dissButton
dissButton.tintColor = .white
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Visited"
navigationController?.navigationBar.barTintColor = .blue
print("view didload")
print(poi)
}
#objc func dismisController() {
presentingViewController?.dismiss(animated: true, completion: nil)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return poi.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)
let point = poi[indexPath.row]
cell.textLabel?.text = point.name
cell.detailTextLabel?.text = "(\(point.coordinate.latitude), \(point.coordinate.longitude))"
return cell
}
}
You may need
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
self.performSegue(withIdentifier:"place",sender:nil)
}
And make sure segue source is connected to the vc itself ( drag it from yellow roundec icon of mapVC inside IB )
When I ran the code, tapping into the map didn't add any object to pointOfInterest, which you are making a loop on loadPointOfInterests to add an object to poi but its empty, so make sure to add a Place object in poi
In VisitationTableViewController, make sure to add
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "locationCell")
}
I'm trying to show my custom view on click of pin in google map. I have mad my custom view for it in xib file and call that file in the delegate method of func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { But when i run the app and tap the marker it does not show my view. How can i show this? This is my code for custom Xib file,
class MapMarkerWindow: UIView {
#IBOutlet weak var saloonImage: UIImageView!
#IBOutlet weak var nameLbl: UILabel!
#IBOutlet weak var addressLbl: UILabel!
#IBOutlet weak var descriptionLbl: UILabel!
var spotData: NSDictionary?
class func instanceFromNib() -> UIView {
return UINib(nibName: "MapMarkerWindowView", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
}
}
This is the code in my VC where i'm showing my map,
#IBOutlet weak var customView: UIView!
var mapView : GMSMapView?
var locationManager = CLLocationManager()
private var infoWindow = MapMarkerWindow()
fileprivate var locationMarker : GMSMarker? = GMSMarker()
override func viewDidLoad() {
super.viewDidLoad()
self.infoWindow = loadNiB()
GMSServices.provideAPIKey("AIzaSyBMppjEPlRBHUD4To2KqNLgmhu1QcxHg3g")
let camera = GMSCameraPosition.camera(withLatitude: 31.516331, longitude: 74.342792, zoom: 6)
mapView = GMSMapView.map(withFrame: .zero, camera: camera)
view = mapView
let states = [
State(name: "Hafeez Center", long: 74.342792, lat: 31.516331, snippets: "Lahore"),
State(name: "Kalma Chowk", long: 74.331553, lat: 31.504532, snippets: "Lahore"),
// the other 51 states here...
]
for state in states {
let state_marker = GMSMarker()
state_marker.position = CLLocationCoordinate2D(latitude: state.lat, longitude: state.long)
state_marker.title = state.name
state_marker.snippet = "Hey, this is \(state.snippets)"
state_marker.map = mapView
}
self.mapView?.isMyLocationEnabled = true
//Location Manager code to fetch current location
self.locationManager.delegate = self
self.locationManager.startUpdatingLocation()
}
func loadNiB() -> MapMarkerWindow {
let infoWindow = MapMarkerWindow.instanceFromNib() as! MapMarkerWindow
return infoWindow
}
//Location Manager delegates
private func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0)
self.mapView?.animate(to: camera)
//Finally stop updating location otherwise it will come again and again in this delegate
self.locationManager.stopUpdatingLocation()
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
var markerData : NSDictionary?
if let data = marker.userData! as? NSDictionary {
markerData = data
}
locationMarker = marker
infoWindow.removeFromSuperview()
infoWindow = loadNiB()
guard let location = locationMarker?.position else {
print("locationMarker is nil")
return false
}
// Pass the spot data to the info window, and set its delegate to self
infoWindow.spotData = markerData
//infoWindow.delegate = self
// Configure UI properties of info window
infoWindow.alpha = 0.9
infoWindow.layer.cornerRadius = 12
infoWindow.layer.borderWidth = 2
//infoWindow.infoButton.layer.cornerRadius = infoWindow.infoButton.frame.height / 2
let saloonImage = markerData!["image"]!
let name = markerData!["name"]!
let address = markerData!["address"]!
let description = markerData!["description"]!
infoWindow.addressLbl.text = address as? String
infoWindow.nameLbl.text = name as? String
infoWindow.descriptionLbl.text = description as? String
infoWindow.saloonImage.image = saloonImage as? UIImage
//Offset the info window to be directly above the tapped marker
infoWindow.center = mapView.projection.point(for: location)
infoWindow.center.y = infoWindow.center.y - 82
self.view.addSubview(infoWindow)
return false
}
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
if (locationMarker != nil){
guard let location = locationMarker?.position else {
print("locationMarker is nil")
return
}
infoWindow.center = mapView.projection.point(for: location)
infoWindow.center.y = infoWindow.center.y - 82
}
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
infoWindow.removeFromSuperview()
}
This is how my pin looks when i tap on it.
From your code it seems that there is a problem in loading Nib.
Please check below code.
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
self.tappedmarker = marker
var point = mapView.projection.point(for: marker.position)
point.y = point.y - 110
let camera = mapView.projection.coordinate(for: point)
let position = GMSCameraUpdate.setTarget(camera)
mapView.animate(with: position)
self.customMarker = CustomMarker.instancefromNib() as! CustomMarker
customMarker.layer.cornerRadius = 10.0
customMarker.clipsToBounds = true
self.customMarker.center = mapView.projection.point(for: marker.position)
self.customMarker.center.y = self.customMarker.center.y - 110
self.view.addSubview(self.customMarker)
self.customMarker.bringSubview(toFront: self.view)
return true
}
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
if (tappedmarker != nil) {
guard let location = tappedmarker?.position else {
print("locationMarker is nil")
return
}
customMarker.center = mapView.projection.point(for: location)
customMarker.center.y = customMarker.center.y - 100
}
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
if self.view.subviews .contains(self.customMarker) {
self.customMarker.removeFromSuperview()
return
}
}
Here is the instancefromNib :
class CustomMarker: UIView {
#IBOutlet weak var lblTitle: UILabel!
class func instancefromNib() -> UIView {
return UINib.init(nibName: "CustomMarker", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
}
}
This will look like this :
Hope this will help!
Just set your mapView delegate in viewDidLoad()
self.mapView.delegate = self