2016-07-28 64 views
0

我想在地圖視圖中更改用戶最近的位置圖釘圖像。MKMapView如何更改用戶最近位置的PIN圖像?

「在我的項目中,我在地圖視圖中顯示了一些商店的位置,位置(lat,long)從api中獲得,在這裏我更改了給定的位置pin圖像,但它工作正常。在地圖視圖中更改用戶最近位置的PIN圖像我已經從用戶當前位置獲得距離詳細信息,以指定位置在5英里以下的位置,這些位置的PIN圖像需要更改「

這裏是我的註釋代碼:

//查看註釋代表代碼以更改針圖像。

 -(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. 

希望這將幫助你實現你想要的。

相關問題