OGeek|极客世界-中国程序员成长平台

标题: ios - MapKit 打印注释到 map [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:09
标题: ios - MapKit 打印注释到 map

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

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

这是 .m 文件中的方法:

#import "MapViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView=_mapView;


- (void)placeNSArray *)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)mapViewMKMapView *)mapView didUpdateUserLocationMKUserLocation *)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"OST"];

    [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];

}



Best Answer-推荐答案


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

-(MKAnnotationView *)mapViewMKMapView *)mapView viewForAnnotationid <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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4