采用环境: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=1000 15 } 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、效果图如下