作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Objective-c 在 Xcode 中开发一个应用程序。我的问题是我试图更改自定义图像的原始红色 map 图钉。我有代码,但只有在您按下定位按钮时才会出现图像图钉。我希望在您加载 map View 时出现自定义图钉图像。我在互联网上寻求帮助,但我不知道我该怎么做。
有人能帮我吗?谢谢!
我的 RestMapPin.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface RestMapPin : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
#import "RestMapPin.h"
@implementation RestMapPin
@synthesize coordinate, title, subtitle;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController {
MKMapView *mapView;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton;
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;
-(IBAction)setMap:(id)sender;
-(IBAction)GetLocation:(id)sender;
@end
#import "MapViewController.h"
#import "SWRevealViewController.h"
#import "RestMapPin.h"
@interface MapViewController ()
@end
@implementation MapViewController
@synthesize mapView;
- (void)viewDidLoad {
[super viewDidLoad];
_barButton.target = self.revealViewController;
_barButton.action = @selector(revealToggle:);
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
[self.navigationItem setTitle:@"Mapa"]; /*Cambia el titulo del navigation controller*/
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; /*Cambia el color de las letras del navigation controller bar del menu principal*/
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:27/255.0f green:101/255.0f blue:163/255.0f alpha:1.0f]];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; /*Cambia el color del boton de la izquierda*/
/*80 Grados*/
MKCoordinateRegion region_80_grados = { {0.0, 0.0}, {0.0, 0.0}};
region_80_grados.center.latitude = 40.42871179999999;
region_80_grados.center.longitude = -3.703639100000032;
region_80_grados.span.longitudeDelta = 0.1f;
region_80_grados.span.latitudeDelta = 0.1f;
[mapView setRegion:region_80_grados animated:YES];
RestMapPin *ann_80_grados = [[RestMapPin alloc] init];
ann_80_grados.title = @"80 Grados";
ann_80_grados.subtitle = @"Malasaña";
ann_80_grados.coordinate = region_80_grados.center;
[mapView addAnnotation:ann_80_grados];
/*90 Grados*/
MKCoordinateRegion region_90_grados = { {0.0, 0.0}, {0.0, 0.0}};
region_90_grados.center.latitude = 40.4164161;
region_90_grados.center.longitude = -3.6699459999999817;
region_90_grados.span.longitudeDelta = 0.1f;
region_90_grados.span.latitudeDelta = 0.1f;
[mapView setRegion:region_90_grados animated:YES];
RestMapPin *ann_90_grados = [[RestMapPin alloc] init];
ann_90_grados.title = @"90 Grados";
ann_90_grados.subtitle = @"Retiro";
ann_90_grados.coordinate = region_90_grados.center;
[mapView addAnnotation:ann_90_grados];
/*Barra de Pintxos 3*/
MKCoordinateRegion region_barra_de_pintxos3 = { {0.0, 0.0}, {0.0, 0.0}};
region_barra_de_pintxos3.center.latitude = 40.4364001;
region_barra_de_pintxos3.center.longitude = -3.680874499999959;
region_barra_de_pintxos3.span.longitudeDelta = 0.1f;
region_barra_de_pintxos3.span.latitudeDelta = 0.1f;
[mapView setRegion:region_barra_de_pintxos3 animated:YES];
RestMapPin *ann_barra_de_pintxos3 = [[RestMapPin alloc] init];
ann_barra_de_pintxos3.title = @"BaRRa de Pintxos";
ann_barra_de_pintxos3.subtitle = @"Barrio de Salamanca";
ann_barra_de_pintxos3.coordinate = region_barra_de_pintxos3.center;
[mapView addAnnotation:ann_barra_de_pintxos3];
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation) {
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pin@2x.png"];
}
else {
//[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapView.mapType = MKMapTypeStandard;
break;
case 1:
mapView.mapType = MKMapTypeSatellite;
break;
case 2:
mapView.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
/*Obtener la localización. Código adaptado para funcionar en iOS8*/
-(IBAction)GetLocation:(id)sender {
mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
mapView.showsUserLocation = YES;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
/***************************************/
[super viewDidAppear:YES];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
NSLog(@"%@", [self deviceLocation]);
//View Area
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.001f;
region.span.longitudeDelta = 0.001f;
[mapView setRegion:region animated:YES];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
- (NSString *)deviceLocation {
return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}
- (NSString *)deviceLat {
return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.latitude];
}
- (NSString *)deviceLon {
return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.longitude];
}
- (NSString *)deviceAlt {
return [NSString stringWithFormat:@"%f", self.locationManager.location.altitude];
}
@end
最佳答案
您需要在 viewDidLoad
中编写以下代码方法,因为当 mapview 的 delegate
时图像应用在 pin 上方法viewForAnnotation
叫。
只需在 vc 加载时先配置 mapView。
mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
mapView.showsUserLocation = YES;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
关于ios - 在 map 引脚 Xcode 中使用图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40464834/
我是一名优秀的程序员,十分优秀!