Putting a Point on a Map in the iPhone - More Wally - Wallace B. McClure
in

MoreWally.com

Giving people what they want, More Wally. This is the technical and personal blog site of
Wallace B. (Wally) McClure.

This Blog

Syndication

News

Please goy buy 3-4 copies of my book on MonoTouch titled "Professional iPhone Programming with MonoTouch for .NET/C# Developers." They make great gifts all year round. Plus, I get about $.20 when you buy a copy.

Technical Sites

More Wally - Wallace B. McClure

This blog will have all kinds of posts about Wally McClure. In it, there will be tons of .NET and computer programming posts as well as Wally's views on life in general. As you might guess, this site and blog help you get More Wally in your life. What more could anyone want? iPhone, Android, MonoTouch, MonoDroid, Mobile, HTML5, .NET, ADO.NET, ASP.NET, AJAX, Atlas, Microsoft AJAX Library, ASP.NET AJAX, and Windows Azure............follow me on twitter at Wally

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 runpublic 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, EventArgse) {}; 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.0public 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

 

Twitter Trackbacks for Putting a Point on a Map in the iPhone - More Wally - Wallace B. McClure [morewally.com] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Putting a Point on a Map in the iPhone - More Wally - Wallace B. McClure         [morewally.com]        on Topsy.com

December 29, 2009 8:48 AM
 

MonoTouch.Info said:

Thank you for submitting this entry - Trackback from MonoTouch.Info

December 30, 2009 3:21 PM
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems