|
暱稱:娃娃魚
  
- UID
- 1
- 帖子
- 3117
- 精華
- 16
- 積分
- 22449
- 威望
- 4567
- K 幣
- 17882 金
- 機型
- iPhone3GS
- 越獄
- 無
- 性別
- 男
- 來自
- 台灣.桃園
- 最後登錄
- 2012-2-6
|
1#
發表於 2009-3-11 20:44
| 只看該作者
說明:示範如何取得定位資料並傳送至Goolge Map電子地圖顯示
示範:
iPhone3G內建的定位功能可取得經度、緯度及高度等資料。
當取得定位資料時,系統將自動跳出提示訊息(隱私權保護?)。
取得定位資料的速度超乎意料的快,和使用Google Map的感覺差滿多,這點倒是滿有趣的。
定位後還可以將經緯度傳送給Google Map電子地圖直接顯示當下的所在位置
程式碼:
GPSDemoViewController.h- #import <UIKit/UIKit.h>
- #import <CoreLocation/CoreLocation.h>
- #import <CoreLocation/CLLocationManagerDelegate.h>
- //加入 CLLocationManagerDelegate protocal,不加似乎也沒關係,不過編譯時會提示警告訊息
- @interface GPSDemoViewController : UIViewController<CLLocationManagerDelegate> {
- IBOutlet UITextField *tflongitude; //經度
- IBOutlet UITextField *tflatitude; //緯度
- IBOutlet UITextField *tfaltitude; //高度
- IBOutlet UISwitch *stopenmap;
-
- CLLocationManager *locmanager;
- BOOL wasFound;
- }
- @property (nonatomic,retain) UITextField *tflongitude;
- @property (nonatomic,retain) UITextField *tflatitude;
- @property (nonatomic,retain) UITextField *tfaltitude;
- @property (nonatomic,retain) UISwitch *stopenmap;
- -(IBAction)UpdateGPS:(id)sender;
- @end
複製代碼 GPSDemoViewController.m- #import "GPSDemoViewController.h"
- @implementation GPSDemoViewController
- @synthesize tflongitude,tflatitude,tfaltitude,stopenmap;
- -(IBAction)UpdateGPS:(id)sender{
- locmanager = [[CLLocationManager alloc] init];
- [locmanager setDelegate:self];
- [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
- [locmanager startUpdatingLocation];
- }
- - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- {
- CLLocationCoordinate2D loc = [newLocation coordinate];
-
- tflatitude.text = [NSString stringWithFormat: @"%f", loc.latitude];
- tflongitude.text = [NSString stringWithFormat: @"%f", loc.longitude];
- tfaltitude.text = [NSString stringWithFormat: @"%f", newLocation.altitude];
-
- //將取得的經緯度傳送給Goolge Map電子地圖顯示位置
- if (stopenmap.on)
- {
- NSString *mapUrl = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%f,%f", loc.latitude, loc.longitude];
- NSURL *url = [NSURL URLWithString:mapUrl];
- [[UIApplication sharedApplication] openURL:url];
- }
- }
複製代碼 此範例的程式碼和原始範例雷同,重新撰寫後並沒有太大的不同,原則上看起來只多了中文化及加了一組Switch來決定是否開啟電子地圖,原來在iPhone3G上取得定位資料是這麼的簡單,相當的好玩喔。
原始範例來源:31Days of iPhone Apps 中的Where am I? |
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊
|