import UIKit
import GoogleMaps
import CoreLocation
class ViewController: UIViewController {
let locationManager = CLLocationManager()
var mapView: GMSMapView?
override func viewDidLoad() {
super.viewDidLoad()
GMSServices.provideAPIKey("XYZ")
let camera = GMSCameraPosition.cameraWithLatitude(37.621262,longitude: -122.378945, zoom: 12)
mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
view = mapView
}
}
What should I add to this code?
This should help you,Please check.
import UIKit
import GoogleMaps
import CoreLocation
class ViewController: UIViewController {
let locationManager = CLLocationManager()
var mapView: GMSMapView?
override func viewDidLoad()
{
super.viewDidLoad()
GMSServices.provideAPIKey("XYZ")
let camera = GMSCameraPosition.camera(withLatitude: 37.621262,longitude: -122.378945, zoom: 12)
mapView = GMSMapView.map(withFrame: CGRect.init(), camera: camera)
// Below line is to default show user current location(Bue dot with circular accuracy radius around it)
mapView!.isMyLocationEnabled = true
view = mapView
}
}
Hope that helps.
Related
I'm trying to generate a map using the GoogleMaps SDK, I had previously generated a map using the lines "let map = GMSMapView()" and then setting the view with "self.view = map" and that worked like a charm, but then I switched over to this more complicated format used in a tutorial I am following, and even though the code is the same, mine appears to fail to generate the map :(
Any help would be amazing as I've been struggling with this for a while now and I really can't spot the issue!
//Modules
import GoogleMaps
import SwiftUI
import UIKit
import GoogleMapsUtils
class MapViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
var mapView: GMSMapView!
let locationManager = CLLocationManager()
override func loadView() {
super.loadView() //LoadMap - Default
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
guard let myLoc = locationManager.location?.coordinate else{return}
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
//Camera Focus
let camera = GMSCameraPosition.camera(withLatitude: myLoc.latitude, longitude: myLoc.longitude, zoom: 15.0)
mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
mapView.isMyLocationEnabled = true
mapView.delegate = self
self.view = mapView
}
}
EDIT: Added swiftui bridge viewcontroller below:
import GoogleMaps
import SwiftUI
struct MapViewControllerBridge: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> MapViewController {
let uiViewController = MapViewController()
return uiViewController
}
func updateUIViewController(_ uiViewController: MapViewController, context: Context) {
}
}
you have just assigned the view. instead of that add mapView to view as subview
just replace this line
self.view = mapView with
self.view.addSubview(mapView)
I am trying to use the existing MapKit functionality on an image.
What I am trying to achieve is to drop pins on an image and maybe add notes on these pins.
I have managed to give the user the possibility to add a pin dynamically with a longGesture but I don't know how to achieve the same on an image.
My code is as follows:
import UIKit
import MapKit
class ViewController: UIViewController , MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
var keyLat:String = "49.2768"
var keyLon:String = "-123.1120"
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
longPressRecogniser.minimumPressDuration = 0.5
mapView.addGestureRecognizer(longPressRecogniser)
mapView.mapType = MKMapType.standard
let location = CLLocationCoordinate2D(latitude: CLLocationDegrees(keyLat.toFloat()),longitude: CLLocationDegrees(keyLon.toFloat()))
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "MY Pin"
annotation.subtitle = "On the Map"
mapView.addAnnotation(annotation)
}
#objc func handleTap(_ gestureReconizer: UILongPressGestureRecognizer)
{
let location = gestureReconizer.location(in: mapView)
let coordinate = mapView.convert(location,toCoordinateFrom: mapView)
// Add annotation:
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = "latitude:" + String(format: "%.02f",annotation.coordinate.latitude) + "& longitude:" + String(format: "%.02f",annotation.coordinate.longitude)
mapView.addAnnotation(annotation)
}
var selectedAnnotation: MKPointAnnotation?
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let latValStr : String = String(format: "%.02f",Float((view.annotation?.coordinate.latitude)!))
let lonvalStr : String = String(format: "%.02f",Float((view.annotation?.coordinate.longitude)!))
print("latitude: \(latValStr) & longitude: \(lonvalStr)")
}
}
any help will be really appreciated.
Thanks
George
The same can be achieved by following the same procedure with a few tweaks here and there. I've made a sample ViewController that demonstrates how you can add pointers (UIViews in this case) into a UIImageView.
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView! // Image view
lazy var longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressScreen)) // long press gesture
// MARK: LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setup()
}
// MARK: Functions
private func setup() {
imageView.image = UIImage(named: "photo")
imageView.addGestureRecognizer(longPress) // Adding gesture recognizer
imageView.isUserInteractionEnabled = true // ImageViews are not user interactive by default
}
// UILongPressGestureRecognizer Action
#objc func didLongPressScreen(_ sender: UILongPressGestureRecognizer) {
let location = sender.location(in: self.view) //Getting location
DispatchQueue.main.async {
let pointer = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
pointer.backgroundColor = .red
pointer.center = location // Setting the center of the view to the x,y coordinates of the long press
self.view.addSubview(pointer) // Adding the UIView to the view
}
}
}
The most important part of this is to enable the user interaction for the UIImageView as its. isUserInteractionEnabled is set to false by default. The output of the above can be seen below,
Trying to get the user's location. A confirmation window appears for 1 second and immediately disappears
import UIKit
import MapKit
import CoreLocation
class NearbyViewController: UIViewController, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
}
}
Make
let locationManager = CLLocationManager()
instance variable
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization()
}
}
as it's get deallocated end of viewDidLoad which hides the alert automatically
Probably because the locationManager is getting deallocated when viewDidLoad completes.
Also, I would consider possibly moving to viewWillAppear or viewDidAppear.
Try this:
import UIKit
import MapKit
import CoreLocation
class NearbyViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization()
}
}
Or this:
import UIKit
import MapKit
import CoreLocation
class NearbyViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
locationManager.requestWhenInUseAuthorization()
}
}
problem
I want to make the screen transition when I tap a circle drawn using GMSCircle, not a marker or title.
I wrote the code below, but tapping the circle does not show anything in the debug area and I can not transition.
What is missing?
environment
swift4
Google Maps SDK for iOS
Xcode 9.4.1
import UIKit
import GoogleMaps
import CoreLocation
class ViewController: UIViewController, GMSMapViewDelegate{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// map
let camera = GMSCameraPosition.camera(withLatitude: 35.687396, longitude: 139.743924, zoom: 17)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
self.view = mapView
// mapView.isMyLocationEnabled
let circle = GMSCircle()
circle.position = CLLocationCoordinate2DMake(35.687396, 139.743924)
circle.radius = CLLocationDistance(10)
circle.fillColor = UIColor.blue
circle.isTappable = true
circle.map = mapView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func mapView(_ mapView: GMSMapView, didTapCircle: GMSCircle) {
performSegue(withIdentifier: "goTalk", sender: nil)
}
}
I checked what was happening and I noticed that as you said, it didn't work.
But according to this answer, I noticed that you need this other delegate method:
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
performSegue(withIdentifier: "goTalk", sender: nil)
}
This works propertly.
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!