Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

MKMapView tutorial using latitude and longitude

iamsgtyangiamsgtyang Posts: 24Registered Users
Quick tutorial on how to create a MKMapView that sets its region to a specific latitude and longitude. You can check out the source code at my site if needed.

1. Create New Project, View-based Application, and name it MapTutorial
2. Add a new existing framework: MapKit.framework
3. Add the following to MapTutorialViewController.h

#import
MKMapView *mapView;

The final MapTutorialViewController.h file should look like this

#import
#import

@interface MapTutorialViewController : UIViewController {

MKMapView *mapView;

}

@end

4. Add the following to viewDidLoad in MapTutorialViewController.m

mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.mapType = MKMapTypeHybrid;

CLLocationCoordinate2D coord = {latitude: 61.2180556, longitude: -149.9002778};
MKCoordinateSpan span = {latitudeDelta: 0.2, longitudeDelta: 0.2};
MKCoordinateRegion region = {coord, span};

[mapView setRegion:region];
[self.view addSubview:mapView];

What happened here is we allocated MKMapView to our mapView and set the location and size of the map view to fit the screen’s view bounds. We also declared the mapType to be MKMapTypeHybrid. If you want to know more, right click on MKMapTypeHybrid in your Xcode and search for the term in the documentation.

Now, we can just set the map to display our current location, which is easy but it takes the fun away. This is where the map’s API is tricky. We can set the region to display on the map by Google Map’s latitude and longitude. To do this, we have to get the MKCoordinateRegion for the location we want.

Here’s the long part. We have to know the longitude and latitude of the location from Google Maps. You get this in the URL when you search for a location. This is where CLLocationCoordinate2D coord = {} come in. You will set the latitude and longitude here.

Next the MKCoordinateSpan (defines the area spanned by a map region), I set it at 0.2. MKCoordinateRegion is made of CLLocationCoordinate2D and MKCoordinateSpan.

The reason for all this is because MKMapView’s setRegion parameter requires a MKCoordinateRegion. Finally, we add the mapView as a subview to self.view

The Final MapTutorialViewController.m file should look like this

#import “MapTutorialViewController.h”

@implementation MapTutorialViewController

- (void)viewDidLoad {

[super viewDidLoad];

mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.mapType = MKMapTypeHybrid;

CLLocationCoordinate2D coord = {latitude: 61.2180556, longitude: -149.9002778};
MKCoordinateSpan span = {latitudeDelta: 0.2, longitudeDelta: 0.2};
MKCoordinateRegion region = {coord, span};

[mapView setRegion:region];
[self.view addSubview:mapView];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

- (void)dealloc {

[super dealloc];

}

@end

Now that’s it. Build and Run, you will see a hybrid map at the latitude and longitude provided (Anchorage, Alaska) To change the location, just change the latitude and longitude value at CLLocationCoordinate2D coord = {latitude: value, longitude: value};
Post edited by iamsgtyang on

Replies

Sign In or Register to comment.