我正在使用mapkit。我开发了简单的故事板应用程序。
1 - mapkit应缩放到最大比例,以便在加载mapkit时显示用户位置。
2 - 在点击loc.png时,地图应该加载带有标题和副标题的位置描述以及有关位置的详细信息
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"testAnnotationView"];
if(annotationView == nil){
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"testAnnotationView"];
annotationView.image = [UIImage imageNamed:@"loc.png"];
annotationView.canShowCallout = true;
}
return annotationView;
}
我如何完成这些任务?您可以从此链接下载示例项目。https://drive.google.com/file/d/0B5pNDpbvZ8SnRExkamtmdkwzeWc/view?usp=sharing
答案 0 :(得分:0)
使用mapKit的这个扩展名,并根据需要调整值,如果值越小,则缩放越大
#import <MapKit/MapKit.h>
@interface MKMapView (Zoom)
-(void)zoomToUserLocation;
-(void)zoomToUserLocationWith:(CLLocationCoordinate2D)coordinate and:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters;
-(void)zoomToUserLocationWith:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters;
@end
#import "MKMapView+Zoom.h"
@implementation MKMapView (Zoom)
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(void)zoomToUserLocation
{
[self zoomToUserLocationWith:1000 and:1000];
}
-(void)zoomToUserLocationWith:(CLLocationCoordinate2D)coordinate and:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters
{
[self setRegion:MKCoordinateRegionMakeWithDistance(coordinate, latitudinalMeters, longitudinalMeters)];
}
-(void)zoomToUserLocationWith:(CLLocationDistance)latitudinalMeters and:(CLLocationDistance)longitudinalMeters
{
if(self.userLocation.location != nil){
[self zoomToUserLocationWith:self.userLocation.location.coordinate and:latitudinalMeters and:longitudinalMeters];
}
}
@end
[self.mapView zoomToUserLocation];
或
[self.mapView zoomToUserLocationWith:50 and:50];
或者您可以在
中使用它- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
[mapView zoomToUserLocationWith:view.annotation.coordinate and:500 and:500];
}
extension MKMapView {
func zoomToUserLocation() {
self.zoomToUserLocation(latitudinalMeters: 1000, longitudinalMeters: 1000)
}
func zoomToUserLocation(latitudinalMeters:CLLocationDistance,longitudinalMeters:CLLocationDistance)
{
guard let coordinate = userLocation.location?.coordinate else { return }
let region = MKCoordinateRegionMakeWithDistance(coordinate, latitudinalMeters, longitudinalMeters)
setRegion(region, animated: true)
}
}
mapView.zoomToUserLocation()
或
mapView.zoomToUserLocation(latitudinalMeters:50,longitudinalMeters:50)
希望这有帮助
答案 1 :(得分:0)
这是我编写的用于放大和缩小WKInterfaceMap(WatchKit)的代码,其大小不小于其最小值(0.0)并且不大于其最大值(世界地图的宽度/高度),并且该曲线加速变焦增加,离得越远,减速变焦减小,离得越近:
- (void)crownDidRotate:(WKCrownSequencer *)crownSequencer rotationalDelta:(double)rotationalDelta
{
span.latitudeDelta += ((rotationalDelta * rotationalDelta) * (rotationalDelta)) + (span.latitudeDelta * rotationalDelta);
span.longitudeDelta += ((rotationalDelta * rotationalDelta) * (rotationalDelta)) + (span.longitudeDelta * rotationalDelta);
span.latitudeDelta = (span.latitudeDelta < 0) ? 0 : (span.latitudeDelta > MKCoordinateRegionForMapRect(MKMapRectWorld).span.latitudeDelta) ? MKCoordinateRegionForMapRect(MKMapRectWorld).span.latitudeDelta : span.latitudeDelta;
span.longitudeDelta = (span.longitudeDelta < 0) ? 0 : (span.longitudeDelta > MKCoordinateRegionForMapRect(MKMapRectWorld).span.longitudeDelta) ? MKCoordinateRegionForMapRect(MKMapRectWorld).span.longitudeDelta : span.longitudeDelta;
MKCoordinateRegion visibleRegion = MKCoordinateRegionMake(PlanetaryHourDataSource.sharedDataSource.locationManager.location.coordinate, span);
[self.map setRegion:visibleRegion];
}
这不仅可以防止出现无效区域(并在达到最大和最小缩放级别时向用户指示),而且可以确保在地图显示更多细节时缩放速度降低,反之亦然:
<iframe src="https://player.vimeo.com/video/313729712" width="640" height="1384" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
加速度曲线如下:
以上答案提供了任意值,不一定对应于频谱的两端。