我是 swift 的初学者,并为我们的食谱应用程序实施适用于 iOS 的 Google Maps SDK。它具有像这样显示当前位置附近的超市的功能。
我的应用程序需要在我的应用程序地图中显示多个标记,指示超市在当前位置附近的位置,但我不知道如何申请获取当前位置附近的超市位置。
我正在关注本教程。 https://www.raywenderlich.com/197-google-maps-ios-sdk-tutorial-getting-started
有一些问题。1.原来,这种方式可以实现我想要的吗?我怀疑本教程的方式是否可以做到,因为我认为本教程应该是旧的。2. 显示错误 Use of undeclared type 'GooglePlace
import UIKit
import GoogleMaps
class PlaceMarker: GMSMarker {
// 1
let place: GooglePlace
// 2
init(place: GooglePlace) {
self.place = place
super.init()
position = place.coordinate
groundAnchor = CGPoint(x: 0.5, y: 1)
appearAnimation = .pop
}
}
- 使用未解析的标识符“GoogleDataProvider”
import UIKit
import CoreLocation
import GoogleMaps
class MapViewController: UIViewController {
var mapView: GMSMapView!
private let locationManager = CLLocationManager()
private let dataProvider = GoogleDataProvider()
private let searchRadius: Double = 1000
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// locationManager.delegate = self as? CLLocationManagerDelegate
// locationManager.requestWhenInUseAuthorization()
// mapView.delegate = self as? GMSMapViewDelegate
//
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self as! CLLocationManagerDelegate
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
let camera = GMSCameraPosition.camera(withLatitude: locationManager.location!.coordinate.latitude, longitude: locationManager.location!.coordinate.longitude, zoom: 15);
mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
mapView.delegate = self as! GMSMapViewDelegate
self.view.addSubview(mapView)
mapView.settings.myLocationButton = true
mapView.isMyLocationEnabled = true
mapView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
mapView.topAnchor.constraint(equalTo: view.topAnchor),
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
if CLLocationManager.locationServicesEnabled() {
switch (CLLocationManager.authorizationStatus()) {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
}
} else {
print("Location services are not enabled")
}
}
func showCurrentLocation() {
mapView.settings.myLocationButton = true
let locationObj = locationManager.location as! CLLocation
let coord = locationObj.coordinate
let lattitude = coord.latitude
let longitude = coord.longitude
print(" lat in updating \(lattitude) ")
print(" long in updating \(longitude)")
let center = CLLocationCoordinate2D(latitude: locationObj.coordinate.latitude, longitude: locationObj.coordinate.longitude)
let marker = GMSMarker()
marker.position = center
marker.title = "current location"
marker.map = mapView
//let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: lattitude, longitude: longitude, zoom: Float(5))
//self.mapView.animate(to: camera)
// mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension MapViewController: GMSMapViewDelegate {
}
extension MapViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
return
}
locationManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
self.showCurrentLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
self.showCurrentLocation()
}
}
斯威夫特 5 Xcode 11.3
我感谢并欢迎任何帮助。非常感谢。
添加
我在另一个网站上问了同样的问题并得到了答案“你可以在函数下面制作这部分。如果你将位置和标题作为参数传递给这个函数并重复,你可以部署标记”
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
但我完全不知道如何获得超市的位置。我是否需要使用 json 或 Google Place API 或更多?为了实现他的想法,我想需要得到纬度和经度。我现在很困惑......