Showing a specific location using UIMapKit
I have an iPhone application with a number of views, each view represented a part of the world. When my user taps the button to take them to that view, I want to show a map开发者_运维技巧 with a preset location...i.e. when the tap to go to New York it shows a map of New York
Is this possible to do? Specify a latitude and longitude coordinate? or by some other method?
I have the following code so far which takes my users current location and displays the map of that, and i can choose between hybrid, normal or satellite through a segmented control.
-(IBAction)getLocation {
creteMapView.showsUserLocation = YES;
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *)sender).selectedSegmentIndex)
{
case 0:
{
creteMapView.mapType = MKMapTypeStandard;
break;
}
case 1:
{
creteMapView.mapType = MKMapTypeSatellite;
break;
}
case 2:
{
creteMapView.mapType = MKMapTypeHybrid;
break;
}
}
}
That all works perfectly, but instead of the current location I want to just set a already defined one. Any ideas?
Thanks
add class that implement MKAnnotation protocol like : http://trac.ilesansfil.org/browser/branches/ilesansfil_iphone_app/STABLE_1.3/Models/LocationAnnotation.h
http://trac.ilesansfil.org/browser/branches/ilesansfil_iphone_app/STABLE_1.3/Models/LocationAnnotation.m
import header file and try
CLLocationCoordinate2D coordinate1;
coordinate1.longitude = 3.35;
coordinate1.latitude = 44.85;
LocationAnnotation *loc1 = [[LocationAnnotation alloc] initWithCoordinate:coordinate1];
[creteMapView addAnnotation:loc1];
this will add a pin to your map
You can also remove user location if you want with :
creteMapView.showsUserLocation = NO;
Use setRegion in your MKMapView. It needs a MKCoordinateRegion that you make with MKCoordinateRegionMake with a center coordinate (lat, long) and a span (area covered). You can get the latitude and longitude from an API like Google maps, but that would need a full tutorial.
精彩评论