想要在地图上显示包含用户更新位置的自定义图像

时间:2013-09-04 07:18:58

标签: iphone mapkit core-location

我想在谷歌地图上显示自行车/汽车图像,并通过用户跟随旋转。 怎么可能。我添加了didUpdateToLocation方法来更新用户的位置,并添加了viewForAnnotation来显示自行车的当前位置。但是无法在地图上显示用户遵循的自行车。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *currentLocation = newLocation;
    if(currentLocation != nil)
    {
        CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
         MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinates:location title:@"India" subTitle:@"Sarvopari Mall"];
        [self.myMapView addAnnotation:annotation];
    }
}


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

    MKAnnotationView *result = nil;

    if ([annotation isKindOfClass:[MyAnnotation class]] == NO)
    {
        return result;
    }

    if ([mapView isEqual:self.myMapView] == NO)
    {
        return result;
    }

    MyAnnotation *senderAnnotation = (MyAnnotation *)annotation;

    NSString *pinReusableIdentifier =
    [MyAnnotation
     reusableIdentifierforPinColor:senderAnnotation.pinColor];


    MKAnnotationView *annotationView = (MKAnnotationView *)
    [mapView
     dequeueReusableAnnotationViewWithIdentifier:
     pinReusableIdentifier];

    if (annotationView == nil){
        annotationView =
        [[MKAnnotationView alloc]  initWithAnnotation:senderAnnotation
                                      reuseIdentifier:pinReusableIdentifier];

        annotationView.canShowCallout = YES;

    }

    UIImage *pinImage = [UIImage imageNamed:@"Bike.png"];
    if (pinImage != nil){
        annotationView.image = pinImage;
        annotationView.canShowCallout = YES;
    }

    result = annotationView;

    return result;
}

1 个答案:

答案 0 :(得分:0)

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }

    annotationView.image = [UIImage imageNamed:@"Bike.png"];
    annotationView.annotation = annotation;

    return annotationView;
}