gpt4 book ai didi

ios - MapKit 打印注释到 map

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:19 26 4
gpt4 key购买 nike

我正在尝试在 map 上使用 mapKit 将自定义注释放置在从 NSArray 获取的坐标处。但是,当我运行该应用程序时,注释不会出现。我已经使用 NSLog 检查过,纬度和经度都存在。

我不确定哪里出错了。类中还有另一种方法使用设备位置成功打印注释,不同之处在于此方法我想使用 CLLocationCoordinate2D 的自定义位置。

这是 .m 文件中的方法:

#import "MapViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView=_mapView;


- (void)place:(NSArray *)tweetData
{
NSArray *array = tweetData;

NSDictionary *dict = [array objectAtIndex:0];


NSString *lat = [dict objectForKey:@"lat"];
NSString *lon = [dict objectForKey:@"lon"];

double lat2 = [lat doubleValue];
double lon2 = [lon doubleValue];

NSLog(@"String %@, %@", lat, lon);
NSLog(@"Double %.8f, %.8f", lat2, lon2);

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(lat2, lon2);

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = position;
annotation.title = @"working";
annotation.subtitle = @"working";

[_mapView addAnnotation:annotation];
}

更新:只是为了澄清同一个类中的其他方法工作正常。我只想使用我从数组中获取的不同位置。默认注释很好,我只是想更改位置。

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";

[self.mapView addAnnotation:point];
}

更新 2:这是另一个包含对 place:

调用的类
#import "ViewController.h"
#import "MapViewController.h"
#import <CoreLocation/CoreLocation.h>


@implementation ViewController

@synthesize button=_button;
@synthesize label=_label;
@synthesize tweetId=_tweetId;
@synthesize tweetContent=_tweetContent;
@synthesize connection=_connection;
@synthesize mapView=_mapView;

NSString *tweet;
NSMutableArray *locationArray;

- (IBAction)fetchTweet
{

NSString *sendCo = [[locationArray valueForKey:@"description"] componentsJoinedByString:@","];

NSLog(@"sendCo: %@", sendCo);

NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/Jersey/rest/hello"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];

[request setValue:@"text/html" forHTTPHeaderField: @"Content-Type"];

[request setHTTPBody:[sendCo dataUsingEncoding:NSUTF8StringEncoding]];

NSData *urlData;
NSURLResponse *response;
NSError *error;

urlData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];

tweet = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];

NSArray *jsonData = [NSJSONSerialization JSONObjectWithData:[tweet dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];


NSLog(@"%@", jsonData);

_mapView = [[MapViewController alloc] init];
[_mapView place:jsonData];

}

最佳答案

正如 igor 所说,您应该实现 MKMapViewDelegate 协议(protocol),将您的 Controller 设置为 MKMapView 委托(delegate)并至少实现下一个方法:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString* PinReuseIdentifier = @"YourReuseIdentifier";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier: PinReuseIdentifier];
if (!annotationView){
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: PinReuseIdentifier];
}
//Customize annotationView

return annotationView;
}

并在 MKAnnotationView 中提供您的自定义图钉 View

关于ios - MapKit 打印注释到 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22747425/

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