xcode MapKit iPhone 开发;当前位置和注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4081047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MapKit iPhone Development; Current Location and Annotations
提问by Jon
To put it simply i'm creating an app that i want to display bars and restaurants as annotations on a map within a city but i would also like the app to show the user's current location. I need to know how to add multiple custom annotations easily but also show my current location. Can Anybody Help?
简单地说,我正在创建一个应用程序,我想在城市内的地图上显示酒吧和餐馆作为注释,但我也希望该应用程序显示用户的当前位置。我需要知道如何轻松添加多个自定义注释,同时还要显示我的当前位置。有人可以帮忙吗?
Here's a part of my ViewController.m
这是我的 ViewController.m 的一部分
- (void)viewDidLoad {
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 53.793853;
region.center.longitude = -1.752442;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"Pashas";
ann.subtitle = @"Leeds Road";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{static NSString *defaultPinID = @"com.invasivecode.pin"; pinView = (MKPinAnnotationView)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else {
[mapView.userLocation setTitle:@"I am here"];
}
return pinView;}
返回 pinView;}
回答by
You need to tell the map view to show the user's location (it's not on by default). Add this to the viewDidLoad:
您需要告诉地图视图显示用户的位置(默认情况下不启用)。将此添加到 viewDidLoad:
[mapView setShowsUserLocation:YES];
Note that in the simulator, the "user location" is always Cupertino, CA so you might not see it depending on the map's current center and zoom.
请注意,在模拟器中,“用户位置”始终是加利福尼亚州库比蒂诺,因此根据地图的当前中心和缩放,您可能看不到它。
回答by Tendulkar
for showing the currentLocation use this
显示当前位置使用这个
mapviewObject.showUserLocation = YES;
for(int i=0;i<10;i++)
{
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 53.793853;
region.center.longitude = -1.752442;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"Pashas";
ann.subtitle = @"Leeds Road";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}