採用環境: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、如下