Skip to content
Browse files

Add iOS4 MKPolyline example

  • Loading branch information...
1 parent 4a338e6 commit 280456c699e00eca5a869d2d321ca2b7ea080252 @conceptdev committed Jul 24, 2010
View
20 AppDelegate.cs
@@ -33,6 +33,7 @@ public partial class AppDelegate : UIApplicationDelegate
MapStuff.MapLineSharp.MapLinesViewController viewController1;
MapStuff.DrawMap.DrawGeometryMapViewController viewController2;
+ MapStuff.os4Maps.os4MapsViewController viewController3;
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
@@ -43,13 +44,22 @@ public override bool FinishedLaunching (UIApplication app, NSDictionary options)
viewController1 = new MapStuff.MapLineSharp.MapLinesViewController();
viewController2 = new MapStuff.DrawMap.DrawGeometryMapViewController();
+
- viewController1.TabBarItem = new UITabBarItem ("Show", UIImage.FromFile("MapLineSharp/103-map.png"), 0);
- viewController2.TabBarItem = new UITabBarItem ("Draw", UIImage.FromFile("DrawMap/72-pin.png"), 0);
- // TODO: add iOS4 examples with MKOverlay
-
- tabBarController.ViewControllers = new UIViewController[] {viewController1, viewController2};
+ viewController1.TabBarItem = new UITabBarItem ("Show (OS3)", UIImage.FromFile("MapLineSharp/103-map.png"), 0);
+ viewController2.TabBarItem = new UITabBarItem ("Draw (OS3)", UIImage.FromFile("DrawMap/72-pin.png"), 0);
+ var v = new Version(UIDevice.CurrentDevice.SystemVersion);
+ if (v.Major >= 4)
+ { // create & use the iOS4 stuff
+ viewController3 = new MapStuff.os4Maps.os4MapsViewController();
+ viewController3.TabBarItem = new UITabBarItem ("Show (OS4)", UIImage.FromFile("os4Maps/tabmap.png"), 0);
+ tabBarController.ViewControllers = new UIViewController[] {viewController1, viewController2, viewController3};
+ }
+ else
+ { // otherwise just iOS3
+ tabBarController.ViewControllers = new UIViewController[] {viewController1, viewController2};
+ }
window.AddSubview (tabBarController.View);
window.MakeKeyAndVisible ();
return true;
View
2 DrawMap/LinePolygonAnnotationView.cs
@@ -91,7 +91,7 @@ public override void Draw (RectangleF rect)
CLLocation location = myAnnotation.Points[idx];
PointF point = MainView.MapView.ConvertCoordinate(location.Coordinate, this);
- Debug.WriteLine("Point: {0}, {1}", point.X, point.Y);
+ //Debug.WriteLine("Point: {0}, {1}", point.X, point.Y);
if (idx == 0)
{
View
13 MapStuff.csproj
@@ -10,7 +10,6 @@
<OutputType>Exe</OutputType>
<RootNamespace>MapStuff</RootNamespace>
<SupportedDevices>IPhoneAndIPad</SupportedDevices>
- <MtouchSdkVersion>3.2</MtouchSdkVersion>
<MtouchMinimumOS>3.0</MtouchMinimumOS>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<BundleIdentifier>net.conceptdevelopment.mapstuff</BundleIdentifier>
@@ -29,6 +28,8 @@
<MtouchLink>None</MtouchLink>
<MtouchI18n />
<AssemblyName>MapStuff</AssemblyName>
+ <MtouchSdkVersion>3.2</MtouchSdkVersion>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' ">
<DebugType>none</DebugType>
@@ -40,6 +41,8 @@
<ConsolePause>false</ConsolePause>
<MtouchLink>None</MtouchLink>
<AssemblyName>MapDraw</AssemblyName>
+ <MtouchSdkVersion>3.2</MtouchSdkVersion>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhone' ">
<DebugSymbols>true</DebugSymbols>
@@ -54,6 +57,8 @@
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchI18n />
<AssemblyName>MapStuff</AssemblyName>
+ <MtouchSdkVersion>4.0</MtouchSdkVersion>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
<DebugType>none</DebugType>
@@ -65,6 +70,8 @@
<ConsolePause>false</ConsolePause>
<CodesignKey>iPhone Developer</CodesignKey>
<AssemblyName>MapLineSharp</AssemblyName>
+ <MtouchSdkVersion>3.2</MtouchSdkVersion>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@@ -89,6 +96,7 @@
<Compile Include="DrawMap\GeometryTouchView.cs" />
<Compile Include="DrawMap\GeometryAnnotation.cs" />
<Compile Include="DrawMap\PointAnnotation.cs" />
+ <Compile Include="os4Maps\os4MapsViewController.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
@@ -100,9 +108,12 @@
<Content Include="MapLineSharp\route.csv" />
<Content Include="MapLineSharp\103-map.png" />
<Content Include="DrawMap\72-pin.png" />
+ <Content Include="os4Maps\route.csv" />
+ <Content Include="os4Maps\tabmap.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="MapLineSharp\" />
<Folder Include="DrawMap\" />
+ <Folder Include="os4Maps\" />
</ItemGroup>
</Project>
View
144 os4Maps/os4MapsViewController.cs
@@ -0,0 +1,144 @@
+using System;
+using System.Drawing;
+using System.Collections.Generic;
+using System.Diagnostics;
+using MonoTouch.Foundation;
+using MonoTouch.UIKit;
+using MonoTouch.MapKit;
+using MonoTouch.CoreLocation;
+
+namespace MapStuff.os4Maps
+{
+ public class os4MapsViewController : UIViewController
+ {
+ MKMapView _mapView;
+ public MKPolyline RouteLine;
+ public MKPolylineView RouteLineView;
+ //MKMapRect _routeRect;
+ public MKCoordinateRegion RouteRegion;
+
+ public os4MapsViewController () : base()
+ {}
+
+ public override void ViewDidLoad ()
+ {
+ base.ViewDidLoad ();
+
+ //
+ // Create our map view and add it as as subview.
+ //
+ _mapView = new MKMapView();
+ _mapView.Frame = new RectangleF (0, 0, this.View.Frame.Width, this.View.Frame.Height);
+ View.AddSubview(_mapView);
+ _mapView.Delegate = new MapViewDelegate(this);
+
+ LoadRoute();
+
+ if (RouteLine != null)
+ {
+ _mapView.AddOverlay(RouteLine);
+ }
+ ZoomInOnRoute();
+ }
+
+ void LoadRoute ()
+ {
+ var filePath = NSBundle.MainBundle.PathForResource("route", "csv", "os4Maps","");
+ var fileContents = System.IO.File.ReadAllText(filePath);
+ var pointStrings = fileContents.Split('\n');
+
+ MKMapPoint northEastPoint = new MKMapPoint(), southWestPoint = new MKMapPoint();
+ var pointArr = new List<MKMapPoint>();
+ var coordArr = new List<CLLocationCoordinate2D>();
+
+
+ // start with the widest possible viewport
+ var tl = new CLLocationCoordinate2D (-90, 180);
+ // top left
+ var br = new CLLocationCoordinate2D (90, -180);
+ foreach (var s in pointStrings)
+ {
+ string[] latlonArr = s.Split(',');
+ double latitude = Convert.ToDouble(latlonArr[0]);
+ double longitude = Convert.ToDouble(latlonArr[1]);
+
+ CLLocationCoordinate2D coordinate = new CLLocationCoordinate2D (latitude, longitude);
+
+ // narrow the viewport bit-by-bit
+ tl.Longitude = Math.Min (tl.Longitude, coordinate.Longitude);
+ tl.Latitude = Math.Max (tl.Latitude, coordinate.Latitude);
+ br.Longitude = Math.Max (br.Longitude, coordinate.Longitude);
+ br.Latitude = Math.Min (br.Latitude, coordinate.Latitude);
+
+ coordArr.Add(coordinate);
+
+ // Can't find MonoTouch **MKMapPointForCoordinate**
+// MKMapPoint point = ???????? (coordinate);
+// if (northEastPoint.X == 0 & northEastPoint.Y == 0)
+// {
+// Debug.WriteLine("First Point: {0},{1}",point.X, point.Y);
+// northEastPoint = point;
+// southWestPoint = point;
+// }
+// else
+// {
+// if (point.X > northEastPoint.X)
+// northEastPoint.X = point.X;
+// if(point.Y > northEastPoint.Y)
+// northEastPoint.Y = point.Y;
+// if (point.X < southWestPoint.X)
+// southWestPoint.X = point.X;
+// if (point.Y < southWestPoint.Y)
+// southWestPoint.Y = point.Y;
+// }
+// pointArr.Add(point);
+ }
+ // Can't use MKMapRect without the **MKMapPointForCoordinate**
+ //_routeRect = new MKMapRect (southWestPoint.X, southWestPoint.Y, northEastPoint.X - southWestPoint.X, northEastPoint.Y - southWestPoint.Y);
+ //Debug.WriteLine("RouteRect: {0},{1};{2},{3}", _routeRect.Origin.X, _routeRect.Origin.Y, _routeRect.Size.Width, _routeRect.Size.Height);
+
+ CLLocationCoordinate2D[] clar = coordArr.ToArray();
+ RouteLine = MKPolyline.FromCoordinates (clar);
+ // divide the range by two to get the center
+ var center = new CLLocationCoordinate2D { Latitude = tl.Latitude - (tl.Latitude - br.Latitude) * 0.5, Longitude = tl.Longitude + (br.Longitude - tl.Longitude) * 0.5 };
+ // calculate the span, with 20% margin so pins aren't on the edge
+ var span = new MKCoordinateSpan { LatitudeDelta = Math.Abs (tl.Latitude - br.Latitude) * 1.2, LongitudeDelta = Math.Abs (br.Longitude - tl.Longitude) * 1.2 };
+ RouteRegion = new MKCoordinateRegion { Center = center, Span = span };
+ }
+
+ void ZoomInOnRoute()
+ {
+ //_mapView.SetCenterCoordinate (new CLLocationCoordinate2D(_routeRect.Origin.X, _routeRect.Origin.Y), false);
+ //_routeRect.Size = new MKMapSize(1,1);
+
+ //_mapView.SetVisibleMapRect (_routeRect, false);
+
+ _mapView.SetRegion(RouteRegion, false);
+ }
+
+ class MapViewDelegate : MKMapViewDelegate
+ {
+ os4MapsViewController _viewController;
+ public MapViewDelegate (os4MapsViewController viewController)
+ {
+ _viewController = viewController;
+ }
+ public override MKOverlayView GetViewForOverlay (MKMapView mapView, NSObject overlay)
+ {
+ MKOverlayView overlayView = null;
+ if (overlay == _viewController.RouteLine)
+ {
+ if (null == _viewController.RouteLineView)
+ {
+ _viewController.RouteLineView = new MKPolylineView(_viewController.RouteLine);
+ _viewController.RouteLineView.FillColor = UIColor.Red;
+ _viewController.RouteLineView.StrokeColor = UIColor.Red;
+ _viewController.RouteLineView.LineWidth = 3;
+ }
+ overlayView = _viewController.RouteLineView;
+ }
+ return overlayView;
+ }
+ }
+ }
+}
View
354 os4Maps/route.csv
@@ -0,0 +1,354 @@
+42.33677,-71.126046
+42.336845,-71.126239
+42.336953,-71.12639
+42.337103,-71.12654
+42.337275,-71.126711
+42.337457,-71.126883
+42.337629,-71.127076
+42.3378,-71.127269
+42.337908,-71.127419
+42.338026,-71.12757
+42.338079,-71.127656
+42.338176,-71.127806
+42.338262,-71.127977
+42.338337,-71.12817
+42.338412,-71.128342
+42.338476,-71.128514
+42.338541,-71.128857
+42.338508,-71.129179
+42.338465,-71.129501
+42.338412,-71.129887
+42.338358,-71.130252
+42.338315,-71.130681
+42.338315,-71.130939
+42.338315,-71.131175
+42.338326,-71.131411
+42.338347,-71.131647
+42.33838,-71.131861
+42.338423,-71.132076
+42.338455,-71.13229
+42.338487,-71.132505
+42.338519,-71.13272
+42.338551,-71.132934
+42.338573,-71.13302
+42.338594,-71.133213
+42.338626,-71.133406
+42.338648,-71.133599
+42.338669,-71.133707
+42.338691,-71.133857
+42.338712,-71.134007
+42.338766,-71.134243
+42.338809,-71.134393
+42.338873,-71.134543
+42.338948,-71.134672
+42.339013,-71.134801
+42.339088,-71.134887
+42.33912,-71.13493
+42.339184,-71.135037
+42.339302,-71.135209
+42.33941,-71.13538
+42.339431,-71.135402
+42.339463,-71.135509
+42.339463,-71.13553
+42.339431,-71.135638
+42.339377,-71.135852
+42.339302,-71.136088
+42.339216,-71.136389
+42.339152,-71.136582
+42.339098,-71.136796
+42.339045,-71.137011
+42.339002,-71.137226
+42.338948,-71.137462
+42.338895,-71.137698
+42.338852,-71.137934
+42.338798,-71.138148
+42.338744,-71.138384
+42.338701,-71.138599
+42.33868,-71.138835
+42.338691,-71.138813
+42.338594,-71.13905
+42.33853,-71.139264
+42.338465,-71.139479
+42.338401,-71.139715
+42.338337,-71.139972
+42.338262,-71.14023
+42.338197,-71.140509
+42.338111,-71.140873
+42.338058,-71.141109
+42.338004,-71.141388
+42.33794,-71.141646
+42.337865,-71.141968
+42.3378,-71.14229
+42.337747,-71.142547
+42.337693,-71.142783
+42.337639,-71.142998
+42.337586,-71.143234
+42.337532,-71.143491
+42.337489,-71.143706
+42.337435,-71.143963
+42.337393,-71.144199
+42.33735,-71.144414
+42.337296,-71.144671
+42.337253,-71.144886
+42.337242,-71.144929
+42.337253,-71.145058
+42.337178,-71.14538
+42.337124,-71.145616
+42.337071,-71.145873
+42.337017,-71.146152
+42.336953,-71.146431
+42.336888,-71.14671
+42.336835,-71.146967
+42.336781,-71.147203
+42.336727,-71.147461
+42.336674,-71.147718
+42.336609,-71.147976
+42.336545,-71.148233
+42.336491,-71.148491
+42.336438,-71.148705
+42.336384,-71.14892
+42.336363,-71.149027
+42.33632,-71.14922
+42.336277,-71.149392
+42.336255,-71.149499
+42.336202,-71.149693
+42.33618,-71.1498
+42.336127,-71.150014
+42.336084,-71.150293
+42.336062,-71.150508
+42.336051,-71.150808
+42.336041,-71.151023
+42.33603,-71.15128
+42.336009,-71.151538
+42.335987,-71.151817
+42.335966,-71.152096
+42.335933,-71.152375
+42.33588,-71.152632
+42.335783,-71.15289
+42.33573,-71.152997
+42.335654,-71.153104
+42.335579,-71.153212
+42.335515,-71.153319
+42.335333,-71.153469
+42.335139,-71.153598
+42.334936,-71.153705
+42.334732,-71.153791
+42.334517,-71.153877
+42.334313,-71.153963
+42.33411,-71.154048
+42.333906,-71.154134
+42.333702,-71.15422
+42.333584,-71.154263
+42.333401,-71.154349
+42.332919,-71.154649
+42.332565,-71.155057
+42.332479,-71.155164
+42.332318,-71.155443
+42.332168,-71.155701
+42.332028,-71.155958
+42.33191,-71.156194
+42.331824,-71.156387
+42.331706,-71.156688
+42.331642,-71.156945
+42.331599,-71.157203
+42.331588,-71.157482
+42.33162,-71.157739
+42.331685,-71.157997
+42.331792,-71.158297
+42.332028,-71.158662
+42.332221,-71.158876
+42.332425,-71.159048
+42.332629,-71.159263
+42.332811,-71.159456
+42.332983,-71.159692
+42.333144,-71.159906
+42.333283,-71.160142
+42.333423,-71.160378
+42.333552,-71.160614
+42.333659,-71.160872
+42.333755,-71.161108
+42.333841,-71.161344
+42.333873,-71.161451
+42.333916,-71.161559
+42.333981,-71.161795
+42.334013,-71.162009
+42.334077,-71.162267
+42.334281,-71.162417
+42.334442,-71.16246
+42.334614,-71.162524
+42.334775,-71.16261
+42.334936,-71.162717
+42.335097,-71.162803
+42.335258,-71.162932
+42.335429,-71.163061
+42.335612,-71.163168
+42.335794,-71.163275
+42.335966,-71.163383
+42.336148,-71.163511
+42.33633,-71.163619
+42.336513,-71.163747
+42.336706,-71.163855
+42.336899,-71.16394
+42.337092,-71.164026
+42.337285,-71.164112
+42.337489,-71.164198
+42.337693,-71.164262
+42.337929,-71.16437
+42.338079,-71.164434
+42.338272,-71.164541
+42.338455,-71.164649
+42.338626,-71.164777
+42.338862,-71.16497
+42.339013,-71.165142
+42.339109,-71.165357
+42.339184,-71.165593
+42.339292,-71.16585
+42.339442,-71.165979
+42.33956,-71.166022
+42.339699,-71.166022
+42.339807,-71.166043
+42.339849,-71.166151
+42.339989,-71.166258
+42.340053,-71.16643
+42.340075,-71.166644
+42.340064,-71.166794
+42.340043,-71.166966
+42.34001,-71.167116
+42.339967,-71.167266
+42.339903,-71.167438
+42.339839,-71.167631
+42.339774,-71.167824
+42.339699,-71.16806
+42.339624,-71.168296
+42.339549,-71.168532
+42.339453,-71.16879
+42.339281,-71.169305
+42.339195,-71.169541
+42.339141,-71.169648
+42.339088,-71.169755
+42.339023,-71.169884
+42.338959,-71.169991
+42.338884,-71.170099
+42.338755,-71.170292
+42.338626,-71.170506
+42.338487,-71.170721
+42.338412,-71.170807
+42.338262,-71.170979
+42.338111,-71.171129
+42.337961,-71.171257
+42.337736,-71.171472
+42.337586,-71.171622
+42.337446,-71.171772
+42.337339,-71.171923
+42.337232,-71.172094
+42.337157,-71.172287
+42.337081,-71.172588
+42.33706,-71.172802
+42.337049,-71.173124
+42.337081,-71.173489
+42.337114,-71.173854
+42.337178,-71.174219
+42.337253,-71.174541
+42.337296,-71.174884
+42.337317,-71.175356
+42.337253,-71.175592
+42.337178,-71.175892
+42.337103,-71.17615
+42.337006,-71.176407
+42.336878,-71.176643
+42.336749,-71.176858
+42.336631,-71.177073
+42.336524,-71.177266
+42.336427,-71.17748
+42.336341,-71.177673
+42.336277,-71.177866
+42.336223,-71.178102
+42.336202,-71.178253
+42.33618,-71.178403
+42.336169,-71.178553
+42.336169,-71.178746
+42.336169,-71.178918
+42.33618,-71.179132
+42.336191,-71.179347
+42.336212,-71.179583
+42.336234,-71.179819
+42.336266,-71.180034
+42.336298,-71.18027
+42.336341,-71.180506
+42.336352,-71.180742
+42.336384,-71.180956
+42.336427,-71.181214
+42.33647,-71.181471
+42.336502,-71.181729
+42.336545,-71.181986
+42.336577,-71.182244
+42.336599,-71.18248
+42.336609,-71.182609
+42.336642,-71.182866
+42.336695,-71.183574
+42.336749,-71.184003
+42.336781,-71.184304
+42.336749,-71.185055
+42.336663,-71.185634
+42.336545,-71.186213
+42.336502,-71.186364
+42.336427,-71.186643
+42.336373,-71.186943
+42.336341,-71.187222
+42.336298,-71.187544
+42.336266,-71.187887
+42.336255,-71.188209
+42.336255,-71.188509
+42.336245,-71.18881
+42.336245,-71.189024
+42.336234,-71.189282
+42.336234,-71.189497
+42.336223,-71.189904
+42.336212,-71.190333
+42.336212,-71.190655
+42.336212,-71.190891
+42.336202,-71.191149
+42.336202,-71.191428
+42.336202,-71.191685
+42.336202,-71.1919
+42.336212,-71.192114
+42.336212,-71.192415
+42.33618,-71.192801
+42.336137,-71.193037
+42.336116,-71.193037
+42.336127,-71.193187
+42.336051,-71.193488
+42.335987,-71.193724
+42.335912,-71.194024
+42.335858,-71.19426
+42.335805,-71.194861
+42.33588,-71.19544
+42.336191,-71.195912
+42.336717,-71.196342
+42.337114,-71.196771
+42.337382,-71.197243
+42.337575,-71.197608
+42.337704,-71.197865
+42.337811,-71.198101
+42.337908,-71.198337
+42.33795,-71.198595
+42.337983,-71.198831
+42.338026,-71.199517
+42.338026,-71.200097
+42.338004,-71.200397
+42.337983,-71.200676
+42.33795,-71.200955
+42.337918,-71.201255
+42.337897,-71.201513
+42.337886,-71.20177
+42.337897,-71.202028
+42.337897,-71.202242
+42.337918,-71.202457
+42.33794,-71.202672
+42.337983,-71.202993
+42.337843,-71.203294
+42.337714,-71.203423
+42.337564,-71.203487
+42.337382,-71.20353
+42.337167,-71.203487
+42.337071,-71.203337
View
BIN os4Maps/tabmap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 280456c

Please sign in to comment.
Something went wrong with that request. Please try again.