gpt4 book ai didi

ios - 在当前位置和单独位置 mkmapview 上放置地标

转载 作者:行者123 更新时间:2023-11-28 22:35:19 25 4
gpt4 key购买 nike

我在使用 MKMapView 时遇到了一些问题!我是映射的新手!我正在尝试做什么:

Mapview 加载并在用户当前位置和所选客户的地址上放置一个地标。此代码可以在所选客户的地址上放置一个地标,但我似乎无法让它在用户的当前位置放置一个!

有什么想法吗?

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:postcodeForMap
completionHandler:^(NSArray* placemarks, NSError* error){
NSLog(@"Co-ordinate geocode is %@", [placemarks objectAtIndex:0]);
for (CLPlacemark* aPlacemark in placemarks)

{
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPlacemark *mPlacemark = [[MKPlacemark alloc] initWithCoordinate:mapView.userLocation.location.coordinate addressDictionary:nil];



MKCoordinateRegion region = mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;

[mapView setRegion:region animated:YES];
[mapView addAnnotation:placemark];
[mapView addAnnotation:mPlacemark];

}
}
];

最佳答案

您可以使用以下代码获取用户位置

-(void)viewDidLoad{
CLLocationManager *locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;//THIS IS IMPORTANT
[locationManager startUpdatingLocation];
}

当您告诉位置管理器获取位置时,如果 Allowed locationmanager 调用其委托(delegate)方法,请参见下文

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//newLocation is your current Location

self.userLocation=newLocation;//Once you got the location generate the places..
[self generatePlaces];
[manager stopUpdatingLocation];

}

-(void)generatePlaces{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:postcodeForMap
completionHandler:^(NSArray* placemarks, NSError* error){
NSLog(@"Co-ordinate geocode is %@", [placemarks objectAtIndex:0]);
for (CLPlacemark* aPlacemark in placemarks)

{
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
//EDITED
MKPlacemark *mPlacemark = [[MKPlacemark alloc] initWithCoordinate:self.userLocation.coordinate addressDictionary:nil];



MKCoordinateRegion region = mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;

[mapView setRegion:region animated:YES];
[mapView addAnnotation:placemark];
[mapView addAnnotation:mPlacemark];

}
}
];
}

关于ios - 在当前位置和单独位置 mkmapview 上放置地标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16232207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com