Swift: Google 地图 API 相机和 MapView
Swift: Google Map API Camera and MapView
借助在线帮助,我在我的 iOS 应用程序中成功实现了一些 Google 地图功能。
extension GoogleMapViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWithPlace place: GMSPlace) {
searchController?.active = false
// Do something with the selected place.
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place attributions: ", place.attributions)
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = place.formattedAddress
marker.map = mapView
}
}
我相信我已经从搜索栏中找到了 place.formattedAddress
,但是如何检索它的坐标以便我可以设置相机和标记以显示搜索到的位置?
根据他们的 documentation,您可以使用 place.coordinate
获取位置
let currentPlace: CLLocationCoordinate2D = place.coordinate
然后就可以得到它的经纬度分别为currentPlace.latitude
和currentPlace.longitude
我是这样解决的
func resultsController(resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWithPlace place: GMSPlace) {
searchController?.active = false
// Do something with the selected place.
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place attributions: ", place.attributions)
// Create a GMSCameraPosition that tells the map to display the
let camera = GMSCameraPosition.cameraWithTarget(place.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = place.coordinate
marker.title = place.formattedAddress
marker.map = mapView
}
To set the camera position at particular place , first we have to declare a object of CLLocationCoordinate2D and then get the latitude and longitude of that particular place .
var currentPlace:CLLocationCoordinate2D = place.coordinate
Now you are able to get the latitude and longitude by writing currentPlace.latitude and currentPlace.longitude .
Since you have lat/lon , you can easily set the camera position on that place .
let camera = GMSCameraPosition.cameraWithLatitude(currentPlace.latitude, longitude: currentPlace.longitude, zoom: 6.0)
let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
view = mapView
借助在线帮助,我在我的 iOS 应用程序中成功实现了一些 Google 地图功能。
extension GoogleMapViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWithPlace place: GMSPlace) {
searchController?.active = false
// Do something with the selected place.
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place attributions: ", place.attributions)
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = place.formattedAddress
marker.map = mapView
}
}
我相信我已经从搜索栏中找到了 place.formattedAddress
,但是如何检索它的坐标以便我可以设置相机和标记以显示搜索到的位置?
根据他们的 documentation,您可以使用 place.coordinate
let currentPlace: CLLocationCoordinate2D = place.coordinate
然后就可以得到它的经纬度分别为currentPlace.latitude
和currentPlace.longitude
我是这样解决的
func resultsController(resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWithPlace place: GMSPlace) {
searchController?.active = false
// Do something with the selected place.
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place attributions: ", place.attributions)
// Create a GMSCameraPosition that tells the map to display the
let camera = GMSCameraPosition.cameraWithTarget(place.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = place.coordinate
marker.title = place.formattedAddress
marker.map = mapView
}
To set the camera position at particular place , first we have to declare a object of CLLocationCoordinate2D and then get the latitude and longitude of that particular place .
var currentPlace:CLLocationCoordinate2D = place.coordinate
Now you are able to get the latitude and longitude by writing currentPlace.latitude and currentPlace.longitude .
Since you have lat/lon , you can easily set the camera position on that place .
let camera = GMSCameraPosition.cameraWithLatitude(currentPlace.latitude, longitude: currentPlace.longitude, zoom: 6.0)
let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
view = mapView