因为项目需要为海外提供地图坐标。所以在此介绍一下谷歌地图的相关接入。
谷歌地图Api:developers.google.com/maps/docume…
1、先按照谷歌文档的步骤来,需要你用公司或者是个人账号申请得到一个APi密匙。接着
GoogleMaps就是谷歌map,googlePlaces是我用来获取地理POI的。
在AppDelegate中导入
#import <GoogleMaps/GoogleMaps.h>
#import <GooglePlaces/GooglePlaces.h>
在didFinishLaunchingWithOptions引入
要获取地理POI就得注册CMSPlacesCLient。
2、系统自带CLLocationManager获取经纬度。code如下
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.distanceFilter = 5
locationManager.startUpdatingLocation()
具体用法自行谷歌,遵守CLLocationManagerDelegate协议
extension LocationVC:CLLocationManagerDelegate{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location: CLLocation = locations.last!
currentLatitude = location.coordinate.latitude
currentLongitude = location.coordinate.longitude
locationManager.stopUpdatingLocation()
currentLatitude,currentLongitude获取当前的经纬度。
3、初始化谷歌Map
let camera = GMSCameraPosition.camera(withLatitude: 22.276548, longitude: 114.18417, zoom: 17)
mapView = GMSMapView.map(withFrame: CGRect(x: 25.round, y: 110.round + topSafeAreaHeight, width: 325.round, height: 410.round), camera: camera)
mapView.delegate = self
mapView.mapType = .normal
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
self.view.addSubview(mapView)
第一行就是传入经纬度,可以随便写一个因为后面会回调locationManager该方法,得到精确的经纬度,再赋值。这里不建议设置(0,0)因为在网路不好的情况下不会发生绘图,体验不好。
extension LocationVC:CLLocationManagerDelegate{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location: CLLocation = locations.last!
currentLatitude = location.coordinate.latitude
currentLongitude = location.coordinate.longitude
locationManager.stopUpdatingLocation()
mapView.camera = GMSCameraPosition.camera(withLatitude: currentLatitude, longitude: currentLongitude, zoom: 17)
如上图就获得了精准坐标,绘制完成。
其中有一个很大的问题,就是谷歌地图不准确,会偏移个几百米。原因是我兔的法律规定,GPS坐标是算法加密过的。格式为GCJ-02。而系统获取的GPS坐标格式是WGS-84,谷歌地图的坐标格式也是WGS-84。至此海外的用户使用是精准的,但是想在国内使用就必须要把GPS的WGS-84转成GCJ-02格式。采用
pod 'JZLocationConverter','~> 1.0.0'
gvj02Location = JZLocationConverter.wgs84(toGcj02: location.coordinate)
到这里,谷歌地图终于能在国内显示正确。但是。。。还是会有一个蓝色的原点坐标,谷歌地图也没有提供接口修改。不知道谷歌地图内部做了什么处理,还是干脆就不想让国内用户用?行吧随你,Map完毕。
4、大头钉
mapView.camera = GMSCameraPosition.camera(withLatitude: currentLatitude, longitude: currentLongitude, zoom: 17)
//大头钉
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude:currentLatitude, longitude:currentLongitude)
mapView.selectedMarker = marker
marker.map = mapVie
5、获取地理信息
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { placeMarks, error in
if (error != nil || (placeMarks?.count == 0)) {
// self.titleLabel.text = "您输入的地址找不到!";
// print("\(error?.userInfo)")
}else{
//编码成功,输出查询到的所有地标信息
// for placeMark in placeMarks!{
// print("name = \(placeMark.name),country = \(placeMark.locality),postalCode = \(placeMark.postalCode),ISOcountryCode = \(placeMark.isoCountryCode)")
// }
//显示最前面的地标信息 CLPlacemark代表了定位的所有信息。
let firstPlaceMark = placeMarks?.first
self.titleLabel.text = firstPlaceMark?.name
}
}
注意获取不到地理POI。想要获取POI如下
import GooglePlaces
let placesClient = GMSPlacesClient.shared()
placesClient.currentPlace { likelihoodList, error in
print(error)
if (error != nil) {
print(likelihoodList)
}
}
这里要挂VPN才可以测试。
结束,散花。