MapView上的注释:坐标

时间:2012-06-21 20:03:07

标签: objective-c mkannotation

我有两个NSString s(地址和密钥),其中包含数字形式的坐标(经度和纬度)(34,56789 ...):

NSString *key = [allKeys objectAtIndex:i];
NSObject *obj = [DictionaryMap objectForKey:key];

NSString *address = [NSString stringWithFormat:@"%@", obj];


CLLocationCoordinate2D anyLocation;

anyLocation.latitude = [address doubleValue];

anyLocation.longitude  = [key doubleValue];

MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init]; annotationPoint2.coordinate = anyLocation;

annotationPoint2.title = @"Event";
annotationPoint2.subtitle = @"Microsoft's headquarters2";
[mapView addAnnotation:annotationPoint2]; 

......但我无法理解为什么它不会与写入的坐标在同一点上绘图。我认为这不起作用:

[address doubleValue]  

所以我尝试将其替换为:

location.latitude = NSNumber/NSString

但它会出错。

更新:

查看负载:

 UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
[self.mapView addGestureRecognizer:longPressGesture];

[mapView.userLocation setTitle:@"I am here"];

..然后...

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{
    [self.mapView removeGestureRecognizer:sender];
}
else
{

    // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
    CGPoint point = [sender locationInView:self.mapView];
    CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    // Then all you have to do is create the annotation and add it to the map

MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; annotationPoint.coordinate = locCoord;

    annotationPoint.title = @"Microsoft";
    annotationPoint.subtitle = @"Microsoft's headquarters";
    [mapView addAnnotation:annotationPoint];

NSString *latitude = [[NSString alloc] initWithFormat:@"%f",locCoord.latitude];


    NSString *longitude = [[NSString alloc] initWithFormat:@"%f", locCoord.longitude];

    NSLog(latitude);
    NSLog(longitude);


    [[NSUserDefaults standardUserDefaults]setObject:latitude forKey:@"FolderLatitude"];
     [[NSUserDefaults standardUserDefaults]setObject:longitude forKey:@"FolderLongitude"];
}
}

...然后我将坐标保存在JSON文件中,然后从文件中读取它们。

2 个答案:

答案 0 :(得分:0)

打印出为lat / lon设置的值。听起来这些值没有被正确地转换为双倍。如果字符串不是“xxx.xxx”格式,那么当您致电doubleValue时,它们将无法正确转换为双打。

答案 1 :(得分:0)

最后我明白了:这是让它发挥作用的正确方法:

NSString *myString = (string initialization here)

float stringFloat = [myString floatValue];

anyLocation.longitude = stringFloat;