I have created these two variables
var googleMap = GMSMapView()
#IBOutlet weak var mapView: UIView!
I'm assigning googleMap to mapView in viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
self.mapView = self.googleMap //Throwing error as 'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use'
}
I'm providing key in AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
GMSServices.provideAPIKey("AIzaSyB6aQxEmza-wUpOPU60aREWReRIAEAvrHs")
return true
}
Can anyone point me in the direction to solve this?
You do not assign the GMSMapView to your UIView in that way. Here's how you do it
Change the class of your UIView to GMSMapView
first you should change the class of UIView to the GMSMapView from story board , then you should make a IBOutlet from that view in view controller and set camera and else like this
#IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.camera = camera
}
Related
I'm having trouble understanding how to trigger a view controller when someone taps on an an MKAnnotation with MapKit. I created a View Controller and a segue, gave it a custom ID and added this code:
class ViewController: UIViewController, MKMapViewDelegate {
#IBOutlet weak var MapViewUI: MKMapView!
private func mapView(_ mapView: MKMapView, didSelect view: MKAnnotation) {
self.performSegue(withIdentifier: "ShowClientController", sender: nil);
}
override func viewDidLoad() {
let aClient = Client(title: "Excavators", coordinate: CLLocationCoordinate2D(latitude: 0.0236, longitude: 37.9062), info:"A guy");
MapViewUI.addAnnotation(aClient)
super.viewDidLoad()
}}
And this is the Client class in case you were wondering:
class Client: NSObject, MKAnnotation {
var title: String?;
var coordinate: CLLocationCoordinate2D;
var info: String;
init(title: String, coordinate: CLLocationCoordinate2D, info: String){
self.title = title;
self.coordinate = coordinate;
self.info = info;
}
}
What am I not getting? If anyone can shed some light on this I'd be very grateful. Thank you all.
I have two Controller which is Map and BottomSheet.
Once i click marker in Map I want to change UIlabel text.
BottomsheetController.swift
var Data : [DataClass] = []
class BottomSheetViewController : UIViewController{
#IBOutlet weak var Id: UILabel!
func getid(id : String?){
let kick = id
self.Id.text = kick
}
}
MapViewController.swift
class MapViewController: UIViewController ,GMUClusterManagerDelegate{
#IBOutlet weak var mapView: GMSMapView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addBottomSheetView()
}
// MARK: Click marker
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
if let poiitem = marker.userData as? POIItem {
let bvc = BottomSheetViewController()
bvc.getimei(imei: Data[0].id)
}
return true
}
}
it keeps saying Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Just use segue for passing data between two controller
performSegue(idnitfierName,sender)
And than,
perform(){
let vc = segue.destination as! MapViewController
vc.variableName = variable That you have to pass
}
I am trying to create a view for in my app which shows the location of a restaurant. When the user enters the view, it shows something like this
But instead shows the location of the restaurant with a pin and a more zoomed in view. I am new to MapKit and have not been able to make any progress.
This is what my view consists of.
And this is what my view controller consists of.
import UIKit
import MapKit
class FD1ViewController: UIViewController {
#IBOutlet weak var mapView: MKMapView!
private let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
import MapKit
class FD1ViewController: UIViewController {
#IBOutlet weak var mapView: MKMapView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//Create the pin location of your restaurant(you need the GPS coordinates for this)
let restaurantLocation = CLLocationCoordinate2D(latitude: 111.0, longitude: 111.0)
//Center the map on the place location
mapView.setCenter(restaurantLocation, animated: true)
}
}
This should do it. I hope it helps!
I'm using google map iOS in my app , i want to show custom marker info window.How can i create it?
I'm using storyboards and swift as the language.
I had one such problem with my app.
In your ViewController.swift:
class ViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {
let placeMarker = marker as! PlaceMarker
if let infoView = UIView.viewFromNibName("MarkerInfoView") as? MarkerInfoView {
infoView.nameLabel.text = placeMarker.place.name
if let photo = placeMarker.place.photo {
infoView.placePhoto.image = photo
} else {
infoView.placePhoto.image = UIImage(named: "generic")
}
return infoView
} else {
return nil
}
}
}
2.Then create a UIView class:
import UIKit
class MarkerInfoView: UIView {
#IBOutlet weak var placePhoto: UIImageView!
#IBOutlet weak var nameLabel: UILabel!
}
Then create a xib file named the same as UIView class:
Note that the class name is the same as UIView class. You can add label and images as per your requirement. Connect those IBOutlets to the UIView class as shown.
Hope this helps you. All the Best!
Define a delegate for your GMSMapView and implement this function in your delegate:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
}
You can create the view programmatically inside this function, or through a nib file.
UPDATE
Do not forget to change the marker's infoWindowAnchor:
https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_marker.html#a6668957ca7b6857355ee6cd9ab944a92
It's weird, but when I add a UIPanGestureRecognizer to a view it does not work. It's my view, that white view is a subview of the red view:
Main View:
#IBOutlet weak var redView: UIView!
#IBOutlet weak var whiteView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
whiteView.gestureRecognizers = [panRecognizer]
}
func handlePan(recognizer:UIPanGestureRecognizer) {
//foo
}
When I tap and drag the white view it won't move.
try this:
whiteView.isUserInteractionEnabled = true
From the Apple's docs:
If the code for your pan gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:
The isUserInteractionEnabled property of the view is set to true. Image views and labels set this property to false by default.
The number of touches is between the values specified in the minimumNumberOfTouches and maximumNumberOfTouches properties.
For a UIScreenEdgePanGestureRecognizer object, the edges property is configured and touches start at the appropriate edge.
Handling Pan Gestures
Here is the answer:
class ViewController: UIViewController {
#IBOutlet weak var redView: UIView!
#IBOutlet weak var whiteView: UIView!
var lastLocation:CGPoint = CGPointMake(0, 0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(ViewController.handlePan))
whiteView.addGestureRecognizer(panRecognizer)
}
func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(redView)
whiteView.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Promote the touched view
lastLocation = whiteView.center
}
}
If you are using swift language for method you can't use [].
var PanGesture = UIPanGestureRecognizer(target: self, action: "respondToPanGesture:")
whiteView.addGestureRecognizer(PanGesture)
Hope it helps.