Follow me on Twitter at Twitter.com/wbm
FYI, I'm blogging most of my stuff over at More Wally now.
You might want to add my rss feed to your reader at:http://morewally.com/cs/blogs/wallym/rss.aspx
Putting a Point on a Map in the iPhone - Wallace B. McClure

Wallace B. McClure

All About Wally McClure - The musings of Wallym on Web, HTML5, Mobile, Xamarin.iOS, Xamarin.Android, and Windows Azure.

News

Visual Studio Magazine Column Personal Blog

.NET

Book Authors

Business

Family

Friends

Georgia Tech Bloggers

Personal

Archives

Putting a Point on a Map in the iPhone

I wrote the following code to put a point on a map on the iPhone.  It works pretty well.  Basically, I draw a map, then I inherit from the MKAnnotation object and create a new constructor, go out to geocoder and get a lat lon to senter the map on, and finally, I put a point in the center of the map.  I've got to thank Craig Dunn for the inspiration of inheriting from the MKAnnotation object.

using System;

using System.Collections.Generic;

using System.Linq;

using MonoTouch.Foundation;

using MonoTouch.UIKit;

using MonoTouch.MapKit;

using MonoTouch.CoreLocation;

 

namespace Json

{

public class Application

{

static void Main (string[] args)

{

UIApplication.Main (args);

}

}

 

// The name AppDelegate is referenced in the MainWindow.xib file.

public partial class AppDelegate : UIApplicationDelegate

{

// This method is invoked when the application has loaded its

// UI and is ready to run

public override bool FinishedLaunching (UIApplication app, NSDictionary options)

{

// If you have defined a view, add it here:

// window.AddSubview (navigationController.View);

window.MakeKeyAndVisible ();

 

MapVw.ZoomEnabled = true;

MapVw.UserInteractionEnabled = true;

MapVw.ScrollEnabled = true;

MapVw.ShowsUserLocation = true;

 

btnMap.TouchDown += BtnMapTouchDown;

 

LocationTF.EditingDidEnd += delegate(object sender, EventArgs e) {

       UITextField utf = sender as UITextField;

       Console.WriteLine("EditingDidEnd is finished.");

       utf.ResignFirstResponder();

};

 

LocationTF.Ended += delegate(object sender, EventArgs e) {

};

 

LocationTF.EditingDidEndOnExit += delegate(object sender, EventArgs

e) {

};

 

return true;

}

 

void BtnMapTouchDown (object sender, EventArgs e)

{

GeoCode gCode = new GeoCode();

string location = LocationTF.Text;

double lat = 0.0, lon = 0.0, radius = 0.0;

Location.ResignFirstResponder();

gCode.GetLatLon(location, ref lat, ref lon, ref radius);

Console.WriteLine("Lat: " + lat.ToString() + " Lon: " + lon.ToString());

MapVw.Region = new MKCoordinateRegion(new CLLocationCoordinate2D(lat, lon),

                                      new MKCoordinateSpan(.5, .5));

MapVw.ZoomEnabled = true;

MapVw.UserInteractionEnabled = true;

MapVw.ScrollEnabled = true;

btnMap.TouchDown += BtnMapTouchDown;

gCode = null;

}

 

// This method is required in iPhoneOS 3.0

public override void OnActivated (UIApplication application)

{

}

   }

}

The  MKAnnotuation object is inherited like this:

using System;

using MonoTouch.UIKit;

using MonoTouch.MapKit;

using MonoTouch.CoreLocation;

namespace Json

{

// concept borrowed from Craig Dunn’s blog.

   public class ObjAnnotation : MKAnnotation {

      

    private CLLocationCoordinate2D _coordinate;

    private string _title, _subtitle;

//getters must be overridden to return necessary data.

    public override CLLocationCoordinate2D Coordinate {

       get { return _coordinate; }

    }

//title and subtitle are readonly, thus no setter.

    public override string Title {

       get { return _title; }

    }

    public override string Subtitle {

       get { return _subtitle; }

    }

    /// <summary>

    /// Need this constructor to set the fields, since the public

    /// interface of this class is all READ-ONLY

    /// <summary>

    public ObjAnnotation (CLLocationCoordinate2D Coordinate,

                             string Title, string SubTitle) : base()

    {

       _coordinate=Coordinate;

       _title=Title;

       _subtitle=SubTitle;

    }

}

 

}

The result is this:

mapping on the iPhone 

Want to know more about developing with the iPhone?  Check out my Wrox Blox eBook on developing applications with MonoTouch for the iPhone/iPod touch for .NET/C# developers.

Comments

HW said:

Hi

What are the GeoCode and Location classes in this example? I've found the mono touch reverse geocoding classes (MKReverseGeocoding) but can't seem to locate the forwards lookup mechanism

Thanks for the blog posts!

Stuart

# January 9, 2010 4:35 PM