• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - MapKit 打印注释到 map

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:09:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试在 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap