2012-06-21 89 views
0

我有兩個NSString S(地址和密鑰)包含座標(經度和緯度)的數字形式(34,56789 ...):註釋上的MapView:座標

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 

,但它給出了一個錯誤。

UPDATE:

鑑於DID LOAD:

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

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

..then ...

-(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文件,然後讀他們從文件中。

+0

'的NSNumber/NSString'沒有任何意義的ObjC。什麼是錯誤?你認爲會發生什麼?你能解釋一下你想要完成什麼嗎?這不是很清楚。 –

+0

不,我的意思是我先試着用NSNumber(不兼容的參數強),然後是NSString(同樣的錯誤) – Alessandro

回答

0

我終於明白:這是使它工作的正確方法:

NSString *myString = (string initialization here) 

float stringFloat = [myString floatValue]; 

anyLocation.longitude = stringFloat; 
0

打印出您爲緯度/經度設置的值。這聽起來像這些值沒有被正確地轉換爲雙倍。如果字符串不是「xxx.xxx」格式,那麼當您撥打doubleValue時,它們將不會正確轉換爲雙打。

+0

lat:44.840291,lon:17.841797 – Alessandro

+0

這是你怎麼打印出來的? NSLog(@「lat:%@,lon:%@」,address,key) – logancautrell

+0

NSString * latitude = [[NSString alloc] initWithFormat:@「%f」,locCoord.latitude]; ..... NSLog(latitude); – Alessandro