2016-07-28 6 views
0

マップビューでユーザーに最も近い位置のピン画像を変更したいと考えています。MKMapViewユーザーの最寄りの場所のピン画像を変更する方法は?

"私のプロジェクトでは、店舗の場所を地図表示していますが、場所(緯度、経度)はAPIから取得していますが、ここでピン画像を変更しました。地図ビューでユーザーの最寄りのピン画像を変更します。ユーザーの現在の位置から、指定したAPiの場所までの距離の詳細を取得しています。位置はピン画像を変更する必要がある場所が5マイル未満です」

私の注釈コードは次のとおりです。

//注釈の表示ピン画像を変更するための代表者コード。

 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation 
    { 

[self.annotationCustom_View removeFromSuperview]; 
[self.annotationCurrentLoc_View removeFromSuperview]; 

static NSString *identifier = @"myAnnotation"; 
CustomMapViewAnnotation * annotationView = (CustomMapViewAnnotation *)[self.locationsMap_View dequeueReusableAnnotationViewWithIdentifier:identifier]; 
if (!annotationView) 
{ 
    annotationView = [[CustomMapViewAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 

    if([annotation isKindOfClass:[MKUserLocation class]]) 
    { 
     annotationView.image = [UIImage imageNamed:@"LocationsYour-Current-Location-Icon"]; // User Current Location Image 
    } 
    else 
    { 
dispatch_async(dispatch_get_main_queue(), ^{ 


     for(int i=0;i<[locations_ArrayList count];i++) 
     { 
       MapViewLocationModel *objValue=[locations_ArrayList objectAtIndex:i]; 

      float value = [objValue.kiosk_distance floatValue]; 
       if(value < 5.0) 
       { 

       annotationView.image = [UIImage imageNamed:@"LocationsFridge-Location-Icon"]; // Change the pin image which are the below 5.0 miles distance from the user current locations 

       } 
       else 
       { 
        annotationView.image = [UIImage imageNamed:@"LocationsBlackDot"];  // given api locations pin images 
       } 
     } 


    }); 
     } 
} 

annotationView.canShowCallout = NO; 

return annotationView; 

    } 

これは私のコードです。誰も私にこれを助けることができますか?

答えて

0

は、次の試してみてください。

まずあなたが 5マイル内のすべてのピンを取得する必要があり、特定のマップエリアでピンを表示するためには。

// 1. Set the map zoom area visible of 5 miles: 

     mapView.region = MKCoordinateRegionMakeWithDistance(
      centerCoordinate, 
      1609.344f * miles (5 in your case), 
      1609.344f * miles (5 in your case) 
     ); 

    // 2. Now get the Rect of this map area: 

     MKMapRect mRect = self.map.visibleMapRect; 

    // 3. Get the all pins inside this Rect: 

     NSSet *annotationSet = [myMapView annotationsInMapRect:mRect]; 

     // print number of annotations 
     NSLog(@"Number of annotations in rect: %d", annotationSet.count); 

     // this will return an array from the NSSet 
     NSArray *annotationArray = [annotationSet allObjects]; 

    // 4. Assign some parameter to this annotation, by taking some property in the annotation class. 

    // 5. Now in your MapView Delegate method viewForAnnotation check the parameter and do the need full with the respective pins. 

が、これはあなたが望むものを達成するのに役立ちます願っています。

関連する問題