Google Map定位,大头针(Swift3.0)

来源:互联网 发布:汕头酒店美工招聘 编辑:程序博客网 时间:2024/06/06 14:21

1.cocoapods集成Google Map SDK

platform :ios, '8.1'

use_frameworks!

target 'GoogleMapTest'do

    pod 'GoogleMaps'

    

end


2.plist文件配置

1>LSApplicationQueriesSchemes  array

item1   googlechromes   

item2   comgooglemaps

2>App Transport Security Settings

Allow Arbitrary Loads YES

3>Privacy - Location Always Usage Description

4>Privacy - Location When In Use Usage Description

3.代码

import UIKit

import GoogleMaps

import CoreLocation


class WeizhiViewController:UIViewController,CLLocationManagerDelegate {

    let locationManager =CLLocationManager()

    var currentLocation:CLLocation!

    var lock =NSLock()

    overridefunc viewDidLoad() {

        super.viewDidLoad()

        locationManager.delegate =self

        locationManager.desiredAccuracy =kCLLocationAccuracyBest //定位精确度(最高)一般有电源接入,比较耗电

        //kCLLocationAccuracyNearestTenMeters;//精确到10

        locationManager.distanceFilter =50 //设备移动后获得定位的最小距离(适合用来采集运动的定位)

        locationManager.requestWhenInUseAuthorization()//弹出用户授权对话框,使用程序期间授权(ios8)

        //requestAlwaysAuthorization;//始终授权

        locationManager.startUpdatingLocation()

        print("开始定位》》》")


    }



    overridefunc didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    //委托传回定位,获取最后一个

    func locationManager(_ manager:CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        lock.lock()

        currentLocation = locations.last   //注意:获取集合中最后一个位置

        print("定位经纬度为:\(currentLocation.coordinate.latitude)")

        //一直发生定位错误输出结果为0:原因是我输出的是currentLocation.altitude(表示高度的)而不是currentLoction.coordinate.latitude(这个才是纬度)

        print(currentLocation.coordinate.longitude)

        lock.unlock()


//跳转Google Map

        let camera =GMSCameraPosition.camera(withLatitude:currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude, zoom:12)

//大头针标记

        let coordinate = CLLocationCoordinate2D(latitude:currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)

        setuplocationMarker(coordinate: coordinate)


        let mapView =GMSMapView.map(withFrame:CGRect.zero, camera: camera)

        self.view = mapView



    }

//大头针设置的一些参数

func setuplocationMarker(coordinate:CLLocationCoordinate2D) {

        locationMarker =GMSMarker(position: coordinate)

        locationMarker.map =self.viewas! GMSMapView?

        locationMarker.title ="sasdw"

        locationMarker.appearAnimation =kGMSMarkerAnimationPop

        locationMarker.icon =GMSMarker.markerImage(with:UIColor.blue)

        locationMarker.opacity =0.75

    }


    func locationManager(_ manager:CLLocationManager, didFailWithError error:Error) {

        print("定位出错拉!!\(error)")

    }  


}



4.调试

真机调试,由于Google map需要翻墙才能使用,所以手机上也必须连接VPN,才可以使用

1 0
原创粉丝点击