【发布时间】:2018-01-01 11:05:22
【问题描述】:
我正在开发一个应用程序,其中Google 地图上有一组坐标,用于绘制路线。还有第二个加油站坐标数组。在两个 10,000 个点的数组中。我需要显示第二个数组中偏离 5 英里的路线上的点。我在嵌套数组中提出了这个要求,但是这个计算非常冗长,大约需要五分钟。如何优化并加快计算速度?我将非常感谢任何建议:
这是我的代码
- (void)addRandomPointsOnMap {
CGFloat upperBoundLatitude = 46.80;
CGFloat lowerBoundLatitude = 29.76;
CGFloat upperBoundLongitude = -118.64;
CGFloat lowerBoundLongitude = -75.6;
for (int i = 0; i < 10000; i++) {
TSPoint *randomPoint = [[TSPoint alloc] init];
randomPoint.latitude = [self randomFloatBetween:upperBoundLatitude
and:lowerBoundLatitude];
randomPoint.longitude = [self randomFloatBetween:upperBoundLongitude
and:lowerBoundLongitude];
CLLocation *randomPointLocation = [[CLLocation alloc] initWithLatitude:randomPoint.latitude longitude:randomPoint.longitude];
for (int i = 0; i < _routePoints.count; i++) {
TSPoint *routePoint = _routePoints[i];
CLLocation *routePointLocation = [[CLLocation alloc] initWithLatitude:routePoint.latitude longitude:routePoint.longitude];
NSInteger distanceInMeters = [routePointLocation distanceFromLocation:randomPointLocation];
NSInteger distanceInMiles = distanceInMeters / 1609.344;
if (distanceInMiles < 5) {
GMSMarker *markerT1 = [[GMSMarker alloc] init];
markerT1.position = CLLocationCoordinate2DMake(randomPoint.latitude, randomPoint.longitude);
markerT1.icon = [UIImage imageNamed:@"blue_pin"];
markerT1.groundAnchor = CGPointMake(0.5, 0.5);
markerT1.map = _mapView;
}
}
} }
以及计算有界范围内随机坐标的方法:
- (float)randomFloatBetween:(float)lowerBound and:(float)upperBound {
float diff = upperBound - lowerBound;
return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + lowerBound; }
【问题讨论】:
标签: ios objective-c arrays xcode google-maps