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
}
Related
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 searched on everywhere, but i could not find it. I want to below action.
When I touch the annotation on the map, I want to change text on the view.
I tried below code but this does not work. I simply change text on the screen when annotation pin clicked.
private func mapView(mapView: MKMapView, didSelect view: MKAnnotationView{
hastane_adi_text.text = "HAstane"
}
You can see my "ViewControllerClass" below.
import UIKit
import MapKit
import CoreLocation
class HospitalControlloer: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{
#IBOutlet weak var hastane_adi_text: UILabel!
#IBOutlet weak var map: MKMapView!
#IBOutlet weak var randevu_al_button: UIButton!
#IBOutlet weak var hizala_button: UIButton!
let locationMenager = CLLocationManager()
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
randevu_al_button.layer.cornerRadius = 10;
hizala_button.layer.cornerRadius = 10;
let locationS:CLLocationCoordinate2D = CLLocationCoordinate2DMake(41.169425, 29.056801)
let sd = MKPointAnnotation()
sd.coordinate = locationS
sd.title = "Sarıyer Merkez Hastane"
let locationS2:CLLocationCoordinate2D = CLLocationCoordinate2DMake(41.097076, 29.05341)
let sd2 = MKPointAnnotation()
sd2.coordinate = locationS2
sd2.title = "Sarıyer Baltalimanı Hastane"
map.addAnnotation(sd)
map.addAnnotation(sd2)
self.locationMenager.delegate = self
self.locationMenager.desiredAccuracy = kCLLocationAccuracyBest
self.locationMenager.requestWhenInUseAuthorization()
self.locationMenager.startUpdatingLocation()
self.map.showsUserLocation = true
// Do any additional setup after loading the view.
}
private func mapView(mapView: MKMapView, didSelect view: MKAnnotationView){
hastane_adi_text.text = "HAstane"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func locate_button(_ sender: Any) {
locationMenager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1) )
self.map.setRegion(region, animated: true)
self.locationMenager.stopUpdatingLocation()
}
I think you should change delegate func to
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
hastane_adi_text.text = "HAstane"
}
and add map.delegate = self at viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
.....
}
Hello you need to implement this method func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) and add your viewController as map delegate self.map.delegate = self
I hope this helps you
I've been learning Xcode (swift 1.2) for the past week, I've just started looking into the Map Kit and I've hit a brick wall.
I'm following this tutorial : MapKit Location
I've added the mapkit to my view, and added the following code into the controller:
#IBOutlet weak var mapView: MKMapView!
let regionRadius: CLLocationDistance = 1000
#IBOutlet var menuButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)
centerMapOnLocation(initialLocation)
// Do any additional setup after loading the view.
}
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: false)
}
Yet when I run the app, I see the following image:
Can someone please shed some light into why I'm unable to see the actual map instead of the tiles?
Update
Code below afer feedback from ansers:
#IBOutlet weak var mapView: MKMapView!
#IBOutlet var menuButton: UIBarButtonItem!
var locationManage = CLLocationManager()
var locateCoordinate = CLLocationCoordinate2D()
override func viewDidLoad() {
super.viewDidLoad()
let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)
centerMapOnLocation(initialLocation)
}
func centerMapOnLocation(location: CLLocation) {
var coordin: CLLocationCoordinate2D = location.coordinate
var viewRegion: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(coordin, 500, 500)
var adjustedRegion: MKCoordinateRegion = self.mapView.regionThatFits(viewRegion)
self.mapView.setRegion(adjustedRegion, animated: true)
}
Some troubleshooting ideas:
double check that your IBOutlet is connected
check if your simulator has network connection
make the change in viewDidAppear to get visual feedback
try on actual device
try different location, greater radius
double check that you are not changing the coordinates or zoom level again after viewDidLoad
try zooming out and panning on the map to see if the contents changes
Set the delegate and delegate methods, Use this example Code,
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var mapViewOwn: MKMapView!
var locationManage = CLLocationManager()
var locateCoordinate = CLLocationCoordinate2D()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib..
mapViewOwn.delegate = self
locationManage.delegate = self
if CLLocationManager.authorizationStatus() == .NotDetermined {
self.locationManage.requestWhenInUseAuthorization()
}
mapViewOwn.showsUserLocation = true
mapViewOwn.mapType = MKMapType.Standard
mapViewOwn.zoomEnabled = true
mapViewOwn.scrollEnabled = true
locationManage.distanceFilter = kCLDistanceFilterNone
locationManage.desiredAccuracy = kCLLocationAccuracyBest
}
//delegate Methods:
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
}
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
let region : MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1000, 1000)
mapViewOwn.setRegion(mapViewOwn.regionThatFits(region), animated: true)
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
//code here
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
//code here
}
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.
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)