简体   繁体   English

ios-MapKit打印注释以映射

[英]ios - MapKit print annotation to map

I am trying to place a custom annotation on a map at coordinates taken from an NSArray using mapKit. 我正在尝试使用mapKit在地图上的自定义注释上放置从NSArray取得的坐标。 The annotation does not appear when I run the app though. 但是,当我运行应用程序时,注释不会出现。 I have checked using NSLog, that both latitude and longitude are present. 我使用NSLog进行了检查,是否同时存在纬度和经度。

I'm not sure where it is going wrong. 我不确定哪里出了问题。 There is another method in the class that prints an annotation successfully using the devices location, the difference is in this method I want to use a custom location with CLLocationCoordinate2D. 类中还有另一种方法可以使用设备位置成功打印注释,不同之处在于该方法中我想使用带有CLLocationCoordinate2D的自定义位置。

This is the method from the .m file: 这是.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];    
}

Update: Just to clarify this other method in the same class works fine. 更新:只是为了阐明同一类中的其他方法可以正常工作。 I just want to use a different location that I've taken from the array. 我只想使用从阵列获取的其他位置。 The default annotation is fine I just want to change the location. 默认注释很好,我只想更改位置。

- (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];
}

Update 2: This is the other class which contains the call to place: 更新2:这是另一个包含对place:的调用的类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];

}

As igor says, you should implement MKMapViewDelegate protocol, set your controller as the MKMapView delegate and implement at least the next method: 正如igor所说,您应该实现MKMapViewDelegate协议,将控制器设置为MKMapView委托并至少实现下一个方法:

-(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;
}

And provide your custom pin view inside MKAnnotationView 并在MKAnnotationView中提供您的自定义图钉视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM