Objective c MapView上的注释:坐标
我有两个NSString地址和键,它们以数字3456789的形式包含坐标经度和纬度…:Objective c MapView上的注释:坐标,objective-c,mkannotation,Objective C,Mkannotation,我有两个NSString地址和键,它们以数字3456789的形式包含坐标经度和纬度…: NSString *key = [allKeys objectAtIndex:i]; NSObject *obj = [DictionaryMap objectForKey:key]; NSString *address = [NSString stringWithFormat:@"%@", obj]; CLLocationCoordinate2D anyLocation; anyLocation.l
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文件中,然后从文件中读取它们。打印出您为lat/lon设置的值。听起来这些值没有正确地转换为double。如果字符串不是xxx.xxx格式,那么在调用doubleValue时,它们将无法正确转换为double。最后我明白了:这是正确的工作方式:
NSString *myString = (string initialization here)
float stringFloat = [myString floatValue];
anyLocation.longitude = stringFloat;
NSNumber/NSString在ObjC中没有任何意义。怎么了?你认为会发生什么?你能解释一下你想要完成的总体目标吗?不太清楚。不,我只是说我先用了一个NSNumber不兼容的参数strong,然后是一个NSString相同的错误lat:44.840291,lon:17.841797你是这样打印出来的吗?NSLog@lat:%@,lon:%@,address,keyNSString*纬度=[[NSString alloc]initWithFormat:@%f,locCoord.latitude]。。。。。北纬;哦抱歉:CGPoint=[sender locationInView:self.mapView];CLLocationCoordinate2D locCoord=[self.mapView convertPoint:point to CoordinateFromView:self.mapView]。。。在HandleLongPressGesture下,您可能需要将最后一个参数更改为该点来自的任何视图