返回列表 回覆 發文

GPS 衛星定位展示範例

說明:示範如何取得定位資料並傳送至Goolge Map電子地圖顯示

示範:

iPhone3G內建的定位功能可取得經度、緯度及高度等資料。


當取得定位資料時,系統將自動跳出提示訊息(隱私權保護?)。


取得定位資料的速度超乎意料的快,和使用Google Map的感覺差滿多,這點倒是滿有趣的。


定位後還可以將經緯度傳送給Google Map電子地圖直接顯示當下的所在位置

程式碼:
GPSDemoViewController.h
  1. #import <UIKit/UIKit.h>
  2. #import <CoreLocation/CoreLocation.h>
  3. #import <CoreLocation/CLLocationManagerDelegate.h>

  4. //加入 CLLocationManagerDelegate protocal,不加似乎也沒關係,不過編譯時會提示警告訊息
  5. @interface GPSDemoViewController : UIViewController<CLLocationManagerDelegate> {
  6.         IBOutlet UITextField *tflongitude; //經度
  7.         IBOutlet UITextField *tflatitude;  //緯度       
  8.         IBOutlet UITextField *tfaltitude;  //高度
  9.         IBOutlet UISwitch *stopenmap;
  10.        
  11.         CLLocationManager   *locmanager;
  12.         BOOL                wasFound;
  13. }

  14. @property (nonatomic,retain) UITextField *tflongitude;
  15. @property (nonatomic,retain) UITextField *tflatitude;
  16. @property (nonatomic,retain) UITextField *tfaltitude;
  17. @property (nonatomic,retain) UISwitch *stopenmap;

  18. -(IBAction)UpdateGPS:(id)sender;

  19. @end
複製代碼
GPSDemoViewController.m
  1. #import "GPSDemoViewController.h"

  2. @implementation GPSDemoViewController

  3. @synthesize tflongitude,tflatitude,tfaltitude,stopenmap;

  4. -(IBAction)UpdateGPS:(id)sender{
  5.         locmanager = [[CLLocationManager alloc] init];
  6.         [locmanager setDelegate:self];
  7.         [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];       
  8.         [locmanager startUpdatingLocation];
  9. }

  10. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
  11. {
  12.         CLLocationCoordinate2D loc = [newLocation coordinate];
  13.        
  14.         tflatitude.text = [NSString stringWithFormat: @"%f", loc.latitude];
  15.         tflongitude.text        = [NSString stringWithFormat: @"%f", loc.longitude];
  16.         tfaltitude.text = [NSString stringWithFormat: @"%f", newLocation.altitude];
  17.        
  18.         //將取得的經緯度傳送給Goolge Map電子地圖顯示位置
  19.         if (stopenmap.on)
  20.         {
  21.                 NSString *mapUrl = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%f,%f", loc.latitude, loc.longitude];
  22.                 NSURL *url = [NSURL URLWithString:mapUrl];
  23.                 [[UIApplication sharedApplication] openURL:url];
  24.         }
  25. }
複製代碼
此範例的程式碼和原始範例雷同,重新撰寫後並沒有太大的不同,原則上看起來只多了中文化及加了一組Switch來決定是否開啟電子地圖,原來在iPhone3G上取得定位資料是這麼的簡單,相當的好玩喔。

原始範例來源:31Days of  iPhone Apps 中的Where am I?
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

[會心一笑]:
babyfish0226大的教學很棒~ 大大這篇教學是需要放到iphone3G上吧 ? 請教怎麼樣可以把自已開發的軟體  放到iphone裡?   是需要認證嗎? 謝謝!
[b].:VC:.[/b]
[會心一笑]:
定位功能,也許一代也適用,至於iPod Touch那一定是不行的。

想在實機上測試,您可以向Apple申請 IDP 或參考台灣版友pohuang在友站所分享的這一篇 iPhone 實機開發 Part 1 - HelloWorld

[會心一笑]:
babyfish0226大的教學很棒~ 大大這篇教學是需要放到iphone3G上吧 ? 請教怎麼樣可以把自已開發的軟體  放到iphone裡?   是需要認證嗎? 謝謝!
visioncan 發表於 2009-3-11 21:19
不管是不是要賣,如果未來自己開發的軟體想放到App Store, 那還是乖乖跟Apple買單 弄個認證玩才有機會
當然,能賣多少 要看創意...畢竟軟體被破解放出 好像太過容易..
如果是自己想玩 沒有想要賣的意思  那就自己玩破解省錢的方法
定位功能,也許一代也適用,至於iPod Touch那一定是不行的。

想在實機上測試,您可以向Apple申請 IDP 或參考台灣版友pohuang在友站所分享的這一篇 iPhone 實機開發 Part 1 - HelloWorld ...
babyfish0226 發表於 2009-3-11 21:31
第一代沒內建 ,若外接的話
資料的取得可能就沒二代這種內建又可能偷偷先啟動GPS來的快了
[會心一笑]:
1# babyfish0226

請問一下,在內建地圖上的GPS誤差值是怎麼算出來的?
[會心一笑]:
誤差值其實是指定位的準確度,而這就牽扯到GPS的原理,這裡先略過不提...
如果在實用上,你在callback(delegate) function裡面會拿到accuracy,單位是公尺,
就是該裝置的位置應該在GPS座標為中心,以accuracy為半徑的圓球(不是圓圈喔~!)範圍內 ^^

另外就是...CLLocationManager跟NSTimer並用的話要小心喔,有可能會造成RunLoop的crash
2

評分次數

@property (nonatomic,retain) UITextField *tflongitude;
@synthesize tflongitude,tflatitude,tfaltitude,stopenmap;

可以說明一下這兩行的功能好嗎?各範例幾乎都有,但我搞不清楚這兩條的意思?謝謝!!
".objc_class_name_CLLocationManager", referenced from:
      literal-pointer@__OBJC@__cls_refs@CLLocationManager in MainView.o
  "_kCLLocationAccuracyBest", referenced from:
      _kCLLocationAccuracyBest$non_lazy_ptr in MainView.o
我在測試的時候發生了這兩個錯誤,是我少引用了哪些東西呢?另外,範例中- (void)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)oldLocation  應該是在更新的時候會輩呼叫到,這是在拿裡定義的呢?
[會心一笑]:
@property (nonatomic,retain) UITextField *tflongitude;
@synthesize tflongitude,tflatitude,tfaltitude,stopenmap;

可以說明一下這兩行的功能好嗎?各範例幾乎都有,但我搞不清楚這兩條的意思?謝謝!! ...
samcheng62 發表於 2009-4-10 15:31
這兩行是iPhone開發的基礎知識喔,第一行講的是,我要讓tflongitude提供存取的能力(可設定為assign, retain, and copy或readonly),而第二行則是實作tflongitude的存取功能。(應該是這樣吧?用習慣了反而忘了該如何去解釋)

基本上您要讓您的物件擁有【存】、【取】的能力,依規則設定這兩段就對了。

[會心一笑]:
".objc_class_name_CLLocationManager", referenced from:
      literal-pointer@__OBJC@__cls_refs@CLLocationManager in MainView.o
  "_kCLLocationAccuracyBest", referenced from:
      _kCLLocationAccuracy ...
samcheng62 發表於 2009-4-10 17:18
您要手動加入CoreLocation.framework到您的專案中。

[會心一笑]:
謝謝您的回覆。
[會心一笑]:
@property (nonatomic,retain) UITextField *tflongitude;
@synthesize tflongitude,tflatitude,tfaltitude,stopenmap;

可以說明一下這兩行的功能好嗎?各範例幾乎都有,但我搞不清楚這兩條的意思?謝謝!! ...
samcheng62 發表於 2009-4-10 15:31
不要想的太複雜
有學過c語言嗎?
只要有個觀念
參數想全域跟跨class使用
就這樣操作
當然這邊必須是指標才可
謝謝,這樣說我就能理解了,感謝!!
[會心一笑]:
在Google map上的圖釘跟直接使用google map api所添加的圖釘不太一樣,
請問那個圖釘要怎麼用來呢?
謝謝
返回列表