0

NSString座標 (経度と緯度) を数値 (34,56789...) の形式で含む2 つの s (アドレスとキー) があります。

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 ファイルに保存し、ファイルから読み取ります。

4

2 に答える 2

0

lat/lonに設定している値を印刷します。これらの値が正しくdoubleに変換されていないようです。文字列が「xxx.xxx」形式でない場合は、を呼び出したときに正しくdoubleに変換されませんdoubleValue

于 2012-06-21T20:10:29.253 に答える
0

最後に私は理解しました:これはそれを機能させる正しい方法です:

NSString *myString = (string initialization here)

float stringFloat = [myString floatValue];

anyLocation.longitude = stringFloat;
于 2012-06-22T09:20:29.080 に答える