市场中的当前位置和自定义地图图钉图像

时间:2017-10-25 07:18:39

标签: ios mapkit

我知道这个问题被问了上千次,我搜索了很多并应用了很多代码,但它并没有起作用。我有mapkit下面有一个显示当前位置的按钮但是当我点击它时它没有显示任何内容我已经为地图引脚设置了我自己的自定义图像但是它也没有显示,我的代码是

对于当前位置,

locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{


 NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{


 NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
    NSString *longi = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    NSLog(@"Longi %@",longi);
    NSString *lat = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    NSLog(@"Longi %@",lat);
}
}

我的自定义地图图片图片的代码就是这个,

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKAnnotationView *view=[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"annoView"];
if (!view) {
    view=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"annoView"];
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }else{
    view.image=[UIImage imageNamed:@"car_marker.png"];
        view.canShowCallout=YES;
    }
}

return view;
}

1 个答案:

答案 0 :(得分:0)

我认为你应该在didUpdateToLocation中调用addAnnotation

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = newLocation.coordinate;
    [self.mapView addAnnotation:annotation];
}