私はswiftの初心者で、レシピアプリにGoogle Maps SDK for iOSを実装しています。このように現在地付近のスーパーマーケットを表示する機能があります。
私のアプリは、アプリのマップで現在の場所の近くのスーパーマーケットの場所を示す複数のマーカーを表示する必要がありますが、現在の場所の近くのスーパーマーケットの場所を取得するために適用する方法がわかりません。
そして、私はこのチュートリアルに従っています。 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
どんな助けにも感謝し、歓迎します。どうもありがとうございました。
添加
別のWebサイトで同じ質問をしたところ、「以下のこの部分を機能させることができます。場所とタイトルをパラメーターとしてこの機能に渡し、それを繰り返すと、マーカーを展開できます」という答えが得られました。
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 などを使用する必要がありますか? 彼のアイデアを実装するには、緯度と経度を取得する必要があると思います。私は今かなり混乱しています....