c#實現iphone上跟蹤裝置移動

來源:互聯網
上載者:User

採用環境:ios 10.7系統 monotouch技術 c#語言 monodevelop開發工具

1、在monodevelop中建立iphone項目,在這兒選擇single view application

在xcode中刪掉所有,拖拽一個viewcontrol 並將其class命名為:showLocal  拖拽其他控制項形成下面的表單

 2、在showLocal.cs中寫下如下代碼:

核心代碼

 1 // This file has been autogenerated from parsing an Objective-C header file added in Xcode. 2 using System; 3 using MonoTouch.CoreLocation; 4 using MonoTouch.Foundation; 5 using MonoTouch.UIKit; 6  7 namespace Mapping02s { 8  9           public partial class showLocal : UIViewController {10                      enum CLLocationAccuracy {11                                 Best=-1,12                                 NearestTenMeters=10,13                                 HunderdMeters=100,14                                 Kilometer=100015                      }16                      CLLocationManager locMgr;17 18                      public showLocal(IntPtr handle) : base (handle) {19                                 locMgr = new CLLocationManager();20                                 locMgr.DesiredAccuracy = (double)CLLocationAccuracy.Best;21                                 // 預設值是通知所有事件22                                 //    應用程式要求的位置資料的經度層級23                                 locMgr.Delegate = new MyLocalionManagerDelegate(this);24                                 locMgr.DistanceFilter = 10;//在產生更新位置前,裝置必須移動的米數25                                 locMgr.HeadingFilter = 30;//在產生更新的指南針讀數之前裝置需要轉過的度數26                      }27 28                      public override void LoadView() {29                                 base.LoadView();30                                 StartButton.TouchDown += delegate {31                                           locMgr.StartUpdatingLocation();32                                           StopButton.Hidden = false;33                                           StartButton.Hidden = true;34                                 };35                                 StopButton.TouchDown += delegate {36                                           locMgr.StopUpdatingLocation();37                                           StartButton.Hidden = false;38                                           StopButton.Hidden = true;39                                 };40 41                      }42                      private class MyLocalionManagerDelegate:CLLocationManagerDelegate {43                                 private showLocal Sl;44                                 int updateCount = 0;45 46                                 public MyLocalionManagerDelegate(showLocal aa):base() {47                                           Sl = aa;48                                 }49 50                                 public override void UpdatedLocation(CLLocationManager manager, CLLocation newLocation, CLLocation oldLocation) {51                                           var latLong = Math.Round(newLocation.Coordinate.Latitude, 4) + ","52                                                      + Math.Round(newLocation.Coordinate.Longitude, 4);53                                           var dist = 0.0;54                                           Sl.LocationLabel.Text = latLong;55                                           Sl.AccuracyLabel.Text = newLocation.HorizontalAccuracy.ToString();56                                           Sl.TimestampLabel.Text = newLocation.Timestamp.ToString();57                                           if (oldLocation != null) {58                                                      dist = Math.Round(newLocation.DistanceFrom(oldLocation), 1);59                                           }60                                           var text = "Location: " + latLong + Environment.NewLine;61                                           text += "Accuracy: " + newLocation.HorizontalAccuracy.ToString() + Environment.NewLine;62                                           text += dist + "m from last reading" + Environment.NewLine;63                                           text += newLocation.Timestamp + Environment.NewLine;64                                           text += "___________" + Environment.NewLine;65                                           Sl.HistoryText.Text += text;66                                 }67 68                                 public override void UpdatedHeading(CLLocationManager manager, CLHeading newHeading) {69                                           Sl.HeadingLabel.Text = newHeading.TrueHeading.ToString();70                                           Sl.AccuracyLabel2.Text = Math.Round(newHeading.HeadingAccuracy, 2).ToString();71 72                                 }73 74                                 public override void Failed(CLLocationManager manager, NSError error) {75                                           Sl.LocationLabel.Text = "Failed to get location";76                                 }77                      }78           }79 }

 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.