2017-06-01 27 views
0

我不得不显示用户位置周围的位置。位置信息将从服务器到达。我使用'for'循环来解析位置并添加到带有注释引脚的映射。截至目前,它只显示最后到达的位置。
但是要求是这样的,多个位置应该以用户位置显示为中心点。截至目前,我只能显示一个引脚。请帮助如何实现这一目标?
Apple Watch中的多个位置

for (NSDictionary* dictionary in responseDict) 
{ 
     NSString *latitudeString=[NSString stringWithFormat:@"%@",[dictionary valueForKey:@"LATITUDE"]]; 
     double latitude=[latitudeString doubleValue]; 
     NSString *longitudeString=[NSString stringWithFormat:@"%@",[dictionary valueForKey:@"LONGITUDE"]]; 
     double longitude=[longitudeString doubleValue]; 
     NSLog(@"the LATITUDE AND LONGITUDE is %f, %f",latitude,longitude); 



     CLLocationCoordinate2D locationCoordinate; 
     locationCoordinate.latitude=latitude; 
     locationCoordinate.longitude=longitude; 
     [self.mapView addAnnotation:locationCoordinate withPinColor:WKInterfaceMapPinColorPurple]; 

     MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(0.05, 0.05); 
     [self.mapView setRegion:(MKCoordinateRegionMake(locationCoordinate, coordinateSpan))]; 




    } 
` 
+1

您似乎在添加多个注释,但有效缩放地图只显示最后一个注释 – Paulw11

回答

0

您放大在循环的每个迭代一个位置,所以最终你最终放大的最后一个位置。可悲的是WKInterfaceMap没有showAnnotations方法,因为MKMapView这样做,所以你需要自己编写一个函数来实现显示MKCoordinateRegion包括几个注释。

我没有写任何对象 - 一会儿,但这里的,显示在地图上两个注解,并适用于watchOS在斯威夫特的功能:

func showTwoMapCoordinates(first: CLLocationCoordinate2D, second: CLLocationCoordinate2D)->MKCoordinateRegion{ 
    let center = CLLocationCoordinate2D(latitude: (first.latitude+second.latitude)/2, longitude: (first.longitude+second.longitude)/2) 
    var latDelta:CLLocationDegrees { 
     let delta = abs((first.latitude-second.latitude)*1.4) 
     if delta > 0 { 
      return delta 
     } else { 
      return 0.1 
     } 
    } 
    var lonDelta:CLLocationDegrees { 
     let delta = abs((first.longitude-second.longitude)*1.4) 
     if delta > 0 { 
      return delta 
     } else { 
      return 0.1 
     } 
    } 
    let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta) 
    return MKCoordinateRegion(center: center, span: span) 
} 

要延长这n个点,其中, n> 2,你需要做的就是遍历你想要显示的每一对点,找到最大增量,就像上面的函数一样,并使用上面调用的两个点上的函数设置中心和跨度最大增量。