MKMapView如何更改用户最近位置图钉图像?

时间:2016-07-28 09:29:24

标签: ios objective-c mkmapview

我想在地图视图中更改用户最近的位置图钉图像。

"在我的项目中,我在地图视图中显示了一些商店位置。位置(lat,long)来自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;

    }

这是我的代码。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分: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.

希望这能帮助你实现自己想要的目标。