I'm trying to setup an iOS app with the latest MapBox iOS idk (3.2). How much i seek the internet, I can't find an example how to add a map event to the mapview.
For example: i want to add an event when the map becomes idle. Any suggestions?
UPDATE
I think this is the right method to implement:
func mapView(mapView: MGLMapView, regionDidChangeAnimated animated: Bool) {
}
If you’re asking how to use delegate methods, here’s how:
import Mapbox
// Declare conformance to the MGLMapViewDelegate protocol
class ViewController: UIViewController, MGLMapViewDelegate {
var mapView: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
view.addSubview(mapView)
// Set the delegate property of our map view to self after instantiating it.
mapView.delegate = self
}
func mapView(mapView: MGLMapView, regionDidChangeAnimated animated: Bool) -> Bool {
// look at mapView properties and do something
}
}
See https://www.mapbox.com/ios-sdk/examples/ for examples of how to implement basic features with the Mapbox iOS SDK.
Related
I wanted to code a map app for personal use (iOS) using swift 5. I have already been able to make it working by using a single file, but the code looks like a mess, so I decided to use multiple files and just call functions from the ViewController.
For a simple map view I already made it possible with this code:
//ViewController.swift
import UIKit
import Mapbox
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
mapInit()
// Do any additional setup after loading the view.
}
}
//mapInit.swift
import Foundation
import Mapbox
extension ViewController{
func mapInit(){
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Set the map’s center coordinate and zoom level.
mapView.setCenter(CLLocationCoordinate2D(latitude: 59.31, longitude: 18.06), zoomLevel: 9, animated: false)
view.addSubview(mapView)
}
}
but I tried to change the map style from the ViewController file using
mapView.styleURL = MGLStyle.darkStyleURL
but I got
Use of unresolved identifier mapView
I also tried to use
self.mapView.styleURL = MGLStyle.darkStyleURL
but now I got
Value of type 'ViewController' has no member 'mapView'
I also tried to add
var mapView: MGLMapView!
at the top of the ViewController
but now it crashes with the message
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Finally I tried to initialize the mapView from the ViewController.swift and change the style from the mapInit() but it changed nothing.
Does anyone know how to solve this problem?
If you add this
import Mapbox
import UIKit
class ViewController: UIViewController, MGLMapViewDelegate {
var mapView: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.setCenter(CLLocationCoordinate2D(latitude: 0, longitude: 0), zoomLevel: 5, animated: false)
mapView.delegate = self
view.addSubview(mapView)
to your main ViewController, then you can access the mapview via an extension ViewController.
I'm using xcode 9.0, GoogleMaps SDK 2.5.0 and GooglePlaces 2.5.0.
In the method func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool I'm trying to disable the auto center map, when I select a marker and go back to the old behaviour of only showing the marker and no auto center enabled. The problem is that the marker doesn't appear when I implement that behaviour (lines that are commented), all stack overflow I searched implements those lines. I'm lost
class MapViewController: UIViewController, GMSMapViewDelegate
{
//MARK: Class Life Cycle
#IBOutlet weak var mapView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupUI()
setupMap()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: -Setup
func setupUI() {
navigationController?.hideBar()
}
func setupMap() {
let map = MapManager.sharedInstance.setupMap(view: mapView, latitude: GoogleMap.latitude, longitude: GoogleMap.longitude, zoom: GoogleMap.zoom)
map.delegate = self
mapView.addSubview(map)
MapManager.sharedInstance.setupMapMarkers(map: map, file: File.geoFence, fileType: File.json)
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// mapView.selectedMarker = marker
// return true
return false
}
}
If anyone comes across this odd bug, the odd solution is adding the delegate again:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
mapView.delegate = self
mapView.selectedMarker = marker
return true
return false
}
I created a custom tile service and add it to a mapView then it works fine. then I used the same code and created a framework library with a MapView return type in Xcode 8. then I used a sample test app and import that library to it and I called the method used in library and add it to a mapView. So my problem Is when I call and that method to mapView it displays the MapKit map not my custom map
code used in library
import Foundation
import MapKit
public class mapLib: NSObject{
public class func createMap(mapView: MKMapView) ->MKMapView{
let mapView = mapView
//custom map URL
let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
let overlay = MKTileOverlay(urlTemplate: template)
overlay.canReplaceMapContent = true
mapView.add(overlay, level: .aboveLabels)
return mapView;
}
}
code used in app
import UIKit
import MapKit
import mapLib
class ViewController: UIViewController {
#IBOutlet weak var mapV: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let view = mapLib.createMap(mapView: mapV)
mapV.addOverlays(view.overlays)
//any additional setup after loading the view, typically from a nib.
}
I need to clarify that the way I'm going to approach would work or any other method to do it :)
You are missing add self as delegate and implement func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer method, you can do this in your library
something like this
import Foundation
import MapKit
public class mapLib: NSObject{
public class func createMap(mapView: MKMapView) ->MKMapView{
let mapView = mapView
//custom map URL
let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
let overlay = MKTileOverlay(urlTemplate: template)
overlay.canReplaceMapContent = true
mapView.add(overlay, level: .aboveLabels)
mapView.delegate = self
return mapView;
}
}
extension mapLib : MKMapViewDelegate{
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let overlayTile = overlay as? MKTileOverlay{
let overLayRenderer = MKTileOverlayRenderer(tileOverlay: overlayTile)
return overLayRenderer
}
return MKOverlayRenderer(overlay: overlay)
}
}
I am using Google Map in my app. When I use self.view = map that map in a GMSMapView all is working okay.
But when I create an UIView with IBOutlet (thing with name mapView) and use to show map with markers, all the things i get is just an empty UIView.
So, what's difference is between self.view = map and self.mapView = map? What should I do?
You must use GMSMapViewDelegate and your mapView must be as GMSMapView
import GoogleMaps
class ViewController: UIViewController,GMSMapViewDelegate{
#IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
self.mapView.delegate = self
}
}
I'm starting an iOS project, and when I try to use the user's location, the map seems to reset every time I set the center coordinate. I have a function mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation! where the only thing is mapView.centerCoordinate = userLocation.location.coordinate. When I comment out this code, the map seems to use the same region I had set in a button that zooms in. When I uncomment the code, the map zooms back out to the original setting when the location is updated. The memory use continues to increase each time, which leads me to believe a new map instance is being created over the current one for some reason. I didn't have this problem when I used a single view application, but now I'm using the tabbed view application default. The secondViewController is set to the default when it is created. Pretty much all of the code:
class FirstViewController: UIViewController, MKMapViewDelegate {
#IBOutlet var mapView: MKMapView!
#IBOutlet var startButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate=self
mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func startEnd(sender: AnyObject) {
if (startButton.currentTitle == "Start") {
startButton.setTitle("Running", forState: .Normal)
let userLocation=mapView.userLocation
let region=MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate,2000,2000)
mapView.setRegion(region,animated:true)
} else {
startButton.setTitle("Start", forState: .Normal)
}
}
func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
mapView.centerCoordinate = userLocation.location.coordinate
}
How do I stop the map from resetting the map view region to the default (or, possibly, creating a new map on top of the one I already have)