我有一個MKMapView
,顯示當前用戶的位置。當我點擊導航欄中的按鈕時,我想隨機放置10個引腳。這些可以隨機放置在任何地方,但只能在當前可見地圖區域內和當前位置周圍放置。在MKMapView的當前位置周圍添加隨機MKAnnations
如何去做這樣的事情?有沒有辦法在用戶位置附近有一個長/緯度範圍,但在地圖區域內?那麼,我可以從這個範圍內隨機選擇?
基本上,我需要找到目前的MKCoordinateRegion
有什麼座標可用。
有沒有更好的方法來解決這個問題?
我有一個MKMapView
,顯示當前用戶的位置。當我點擊導航欄中的按鈕時,我想隨機放置10個引腳。這些可以隨機放置在任何地方,但只能在當前可見地圖區域內和當前位置周圍放置。在MKMapView的當前位置周圍添加隨機MKAnnations
如何去做這樣的事情?有沒有辦法在用戶位置附近有一個長/緯度範圍,但在地圖區域內?那麼,我可以從這個範圍內隨機選擇?
基本上,我需要找到目前的MKCoordinateRegion
有什麼座標可用。
有沒有更好的方法來解決這個問題?
我想通了,這裏是我是如何做的:
/**
* Adds new pins to the map
*
* @version $Revision: 0.1
*/
+ (void)addPinsToMap:(MKMapView *)mapView amount:(int)howMany {
//First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));
//Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];
CLLocationCoordinate2D swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];
// Loop
for (int y = 0; y < howMany; y++) {
double latRange = [MapUtility randomFloatBetween:neCoord.latitude andBig:swCoord.latitude];
double longRange = [MapUtility randomFloatBetween:neCoord.longitude andBig:swCoord.longitude];
// Add new waypoint to map
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latRange, longRange);
MPin *pin = [[MPin alloc] init];
pin.coordinate = location;
[mapView addAnnotation:pin];
}//end
}//end
/**
* Random numbers
*
* @version $Revision: 0.1
*/
+ (double)randomFloatBetween:(double)smallNumber andBig:(double)bigNumber {
double diff = bigNumber - smallNumber;
return (((double) (arc4random() % ((unsigned)RAND_MAX + 1))/RAND_MAX) * diff) + smallNumber;
}//end
可以使用
看看下面的代碼,用戶位置和其他位置之間的距離,讓您的註解
#define DISTANCE_RADIUS 10.0 // in KM
-(NSArray *)findNearMe:(NSArray *)lounges {
NSMutableArray *arNearme = [NSMutableArray array];
CLLocation *currentLocation;
for(NSDictionary *d in lounges)
{
currentLocation = [[CLLocation alloc] initWithLatitude:29.33891 longitude:48.077202];
CGFloat latitude=[[d valueForKey:@"latitude"] floatValue];
CGFloat longitude=[[d valueForKey:@"longitude"] floatValue];
CLLocation *newPinLocation=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
double distanceValue=[currentLocation distanceFromLocation:newPinLocation];
if(distanceValue/1000.0<=DISTANCE_RADIUS) {
[arNearme addObject:d];
}
}
return arNearme;
}
它會返回用戶位置附近1到1000公里範圍內的數組。 使用註釋數組並顯示註釋以與用戶位置一起映射視圖。
在夫特3.0
func addPins() {
let nePoint = CGPoint(x: mapView.bounds.origin.x + mapView.bounds.size.width, y: mapView.bounds.origin.y)
let sePoint = CGPoint(x: mapView.bounds.origin.x, y: mapView.bounds.origin.y + mapView.bounds.size.height)
let neCoord = mapView.convert(nePoint, toCoordinateFrom: mapView)
let seCoord = mapView.convert(sePoint, toCoordinateFrom: mapView)
var y = 5
while y > 0 {
let latRange = randomBetweenNumbers(firstNum: Float(neCoord.latitude), secondNum: Float(seCoord.latitude))
let longRange = randomBetweenNumbers(firstNum: Float(neCoord.longitude), secondNum: Float(seCoord.longitude))
let location = CLLocationCoordinate2D(latitude: CLLocationDegrees(latRange), longitude: CLLocationDegrees(longRange))
let pin = MKPointAnnotation()
pin.coordinate = location
pin.title = "Home"
mapView.addAnnotation(pin)
y -= 1
}
}
func randomBetweenNumbers(firstNum: Float, secondNum: Float) -> Float{
return Float(arc4random())/Float(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}