gpt4 book ai didi

iphone - 如何在 iPhone 应用程序的单个 map View 中显示多个位置

转载 作者:行者123 更新时间:2023-12-03 20:51:40 27 4
gpt4 key购买 nike

我有 NSMutableArray,其中包含多个地点 - 名称、纬度和经度。这些位置值在距当前位置 5000 米的范围内。我需要在单个 map 中显示所有位置的注释图钉。这可能吗?

提前致谢..

最佳答案

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : 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 "MyAnnotation.h"

@implementation MyAnnotation

@synthesize title;
@synthesize subtitle;
@synthesize coordinate;

- (void)dealloc
{
[super dealloc];
self.title = nil;
self.subtitle = nil;
}
@end

//in your controller in .h
MyAnnotation* myAnnotation;
NSMutableArray *annotations;

//im .m file
- (void)viewDidLoad
{
[super viewDidLoad];
mapp.showsUserLocation = YES;

mapp.delegate = self;

annotations = [[NSMutableArray alloc] init];
for (int i=0; i<[arrayLatitude count]; i++)
{
CLLocationCoordinate2D theCoordinate1;

theCoordinate1.latitude = [[arrayLatitude objectAtIndex:i] floatValue];
theCoordinate1.longitude = [[arrayLongitude objectAtIndex:i] floatValue];

myAnnotation = [[MyAnnotation alloc] init];
myAnnotation.coordinate = theCoordinate1;
myAnnotation.title = [arrayName objectAtIndex:i];
[mapp addAnnotation:myAnnotation];
[annotations addObject:myAnnotation]

}


NSLog(@"%d",[annotations count]);


MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo))
{
flyTo = pointRect;
}
else
{
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
mapp.visibleMapRect = flyTo;

}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;

static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorRed;

UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",rand()%3908+1]]];
pinView.leftCalloutAccessoryView = icon;
[icon release];

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:nil forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(myMethod:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;

return pinView;
}


- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"Title : %@",view.annotation.title);
NSLog(@"Latitude : %f", view.annotation.coordinate.latitude);
NSLog(@"Longitude : %f", view.annotation.coordinate.longitude);



}

关于iphone - 如何在 iPhone 应用程序的单个 map View 中显示多个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13762017/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com