iOS開發之地圖與定位,ios開發地圖

來源:互聯網
上載者:User

iOS開發之地圖與定位,ios開發地圖

  無論是QQ還是的移動用戶端都少不了定位功能,之前在demo中沒有添加定位功能,今天就寫個定位的小demo來瞭解一下定位和地圖的東西。地圖和定位看上去是挺高大上一東西,其實用法比TableView簡單多了,下面的Demo是用的iOS中內建的地圖和定位,當然了也可以用第三方的來載入地圖,比如百度地圖啥的,在這就不贅述了。今天的部落客要是介紹MKMapView的使用,MapView的使用和其他組件的用法差不多,MapView用的是委託回調,在使用mapView的Controller中要實現MKMapViewDelegate協議,並註冊委託回調,然後實現相應的回調方法即可。

 

  一,構建Demo的組件

    為了節省時間這個demo中的MapView是用Storyboard拖出來的,storyboard中的結構如下所示,上面是MKMapView用於顯示地圖,下面的labei用於顯示當前經緯度:

 

  三、編寫標註模組的類

    1.首先解釋一下標註模組是什麼東西:標註模組就是顯示當前地理位置的資訊,下面圖種綠色框中是標註模組:

  

  

    2.編寫標註類,標註類必須實現MKAnnotation協議,MyAnnotation.h中的代碼如下:

  

    3.實現協議MyAnnotation中的方法,titile返回的是標題,subTitle是子標題,代碼如下:

  四、編寫ViewController中的代碼

    1.ViewController.h中要引入相應的包和實現相應的協議代碼如下

     

    3.實現委託回調中的標註視圖,代碼如下:

 

    4.擷取地理位置資訊,代碼如下:

1 //實現協議中的方法擷取座標 2 -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 3 { 4 //擷取location 5 CLLocation *location = userLocation.location; 6 7 //通過location擷取經緯度 8 CLLocationCoordinate2D coordinate = location.coordinate; 9 10 11 //顯示經緯度12 [self mapPoint:coordinate];13 14 15 //標記範圍16 MKCoordinateSpan span;17 span.latitudeDelta=0.1;18 span.longitudeDelta=0.1;19 MKCoordinateRegion region={coordinate ,span};20 21 [self.myMapView setRegion:region];22 23 24 25 __block __weak ViewController *copy_self = self;26 27 //解析地理位置28 CLGeocoder *geocoder = [[CLGeocoder alloc] init];29 [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {30 if (placemarks.count > 0) {31 //使用CLPlacemark解析地理位置32 CLPlacemark *placeMark = placemarks[0];33 NSDictionary *addressDictionary = placeMark.addressDictionary;34 35 36 //添加地圖的標註37 MyAnnotation *annotation = [[MyAnnotation alloc] init];38 annotation.subTitle = addressDictionary[@"Name"];39 annotation.coordinate = coordinate;40 41 //添加標註資訊42 [copy_self.myMapView addAnnotation:annotation];43 }44 }];45 46 }View Code

 

  以上是Demo的所有代碼,下面是啟動並執行:

 


ios 軟體開發中,需要在地圖上定位自己位置到一個定點的路線圖怎寫?

用高德地圖SDK吧
 
iOS 百度地圖 定位問題

你這個我遇到過很多次了。 1.可能是手機的網路不穩定 2.手機記憶體不足 3.軟體的更新 4.系統本身的不穩定。 我以前是 1.3.4 這3個問題造成的,你一一的排除的解決下。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.