iOS開(kāi)發(fā)之在Google地圖上顯示所在位置
iOS開(kāi)發(fā)之在Google地圖上顯示所在位置是本文要介紹的內(nèi)容,可以使一行代碼顯示你的位置,iOS開(kāi)發(fā)中的MapKit集成了定位的功能,使用一行代碼就可以在google地圖上展示出自己當(dāng)前的位置,代碼如下:
- -(IBAction) showLocation:(id) sender {
- if ([[btnShowLocation titleForState:UIControlStateNormal]
- isEqualToString:@"Show My Location"]) {
- [btnShowLocation setTitle:@"Hide My Location"
- forState:UIControlStateNormal];
- mapView.showsUserLocation = YES;
- } else {
- [btnShowLocation setTitle:@"Show My Location"
- forState:UIControlStateNormal];
- mapView.showsUserLocation = NO;
- }
- }
關(guān)鍵的代碼就是:mapView.showUserLocation=Y(jié)ES.
使用CLLocationManager和MKMapView
還有就是通過(guò)CoreLocation框架寫代碼去請(qǐng)求當(dāng)前的位置,一樣也非常簡(jiǎn)單:
***步:創(chuàng)建一個(gè)CLLocationManager實(shí)例
- CLLocationManager *locationManager = [[CLLocationManager alloc] init];
第二步:設(shè)置CLLocationManager實(shí)例委托和精度
- locationManager.delegate = self;
- locationManager.desiredAccuracy = kCLLocationAccuracyBest;
第三步:設(shè)置距離篩選器distanceFilter,下面表示設(shè)備至少移動(dòng)1000米,才通知委托更新
- locationManager.distanceFilter = 1000.0f;
或者沒(méi)有篩選器的默認(rèn)設(shè)置:
- locationManager.distanceFilter = kCLDistanceFilterNone;
第四步:?jiǎn)?dòng)請(qǐng)求
- [locationManager startUpdatingLocation];
使用下面代碼停止請(qǐng)求:
- [locationManager stopUpdatingLocation];
CLLocationManagerDelegate委托
這個(gè)委托中有:locationManager:didUpdateToLocation: fromLocation方法,用于獲取經(jīng)緯度。
可以使用下面代碼從CLLocation 實(shí)例中獲取經(jīng)緯度
- CLLocationDegrees latitude = theLocation.coordinate.latitude;
- CLLocationDegrees longitude = theLocation.coordinate.longitude;
使用下面代碼獲取你的海拔:
- CLLocationDistance altitude = theLocation.altitude;
使用下面代碼獲取你的位移:
- CLLocationDistance distance = [fromLocation distanceFromLocation:toLocation];
小結(jié):iOS開(kāi)發(fā)之在Google地圖上顯示所在位置的內(nèi)容介紹完了,本篇文章主要是講解了如何在iOS設(shè)備google地圖上展示自己的當(dāng)前位置,***希望本文對(duì)你有所幫助!