热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

转到/缩放到当前位置功能(MapKit)-Goto/zoomtocurrentlocationfunction(MapKit)

IvegotamapViewwhichzoomstothecurrentlocationusingviewDidLoad:我有一个mapView,它使用viewDidLoa

I've got a mapView which zooms to the current location using viewDidLoad :

我有一个mapView,它使用viewDidLoad缩放到当前位置:

#define METERS_PER_MILE 1609.344

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    mapView.showsUserLocation=TRUE;

    // zoom to  a specific area
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = -28.994167;
    zoomLocation.lOngitude= 134.866944;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1900*METERS_PER_MILE, 1900*METERS_PER_MILE);
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];    

    // make sure the Google water mark is always visible
    mapView.autoresizingMask =
    (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

    [mapView setRegion:adjustedRegion animated:YES];        

    mapView.delegate=self;

    searchBar.delegate = self;
}

This works fine. I've added a search bar and a function to jump to a specific address location. This works fine, too. I now want to add a button to jump back to the current location. Can you give me a hand, please?

这很好用。我添加了一个搜索栏和一个跳转到特定地址位置的函数。这也很好。我现在想要添加一个按钮来跳回当前位置。你可以帮我一把吗?

Cheers

5 个解决方案

#1


10  

You need to set the center of your map to the current location on tap of that button. Say, like this:

您需要将地图的中心设置为点按该按钮的当前位置。说,像这样:

- (IBAction)showCurrentLocation {        
    [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}

#2


2  

you can also try:

你也可以尝试:

mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;

#3


1  

You can link this IBAction to your UIButton, it's going to move the map on the current location and zoom on it.

您可以将此IBAction链接到您的UIButton,它将在当前位置移动地图并放大它。

@IBOutlet weak var mapView: MKMapView!

@IBAction func zoomToUserCurrentLocation(sender: AnyObject) {
    if self.mapView != nil {
        self.mapView.setRegion(MKCoordinateRegionMake(
            self.mapView.userLocation.coordinate, 
            MKCoordinateSpanMake(0.1, 0.1)
        ), animated: true)
    }
}

MKCoordinateSpan defines the area spanned by a map region, smaller these values are, closer you zoom on the map.

MKCoordinateSpan定义了地图区域所跨越的区域,这些值越小,越接近地图。

#4


0  

- (void)showCurrentLocation{

    MKMapPoint annotatiOnPoint= MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
    MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.0, 0.0);
    [self.mapView setVisibleMapRect:zoomRect animated:YES];
}

#5


0  

FOR SWIFT

Add this line in button action yourMKMapView.setUserTrackingMode(.follow, animated: true)

在按钮操作中添加此行yourMKMapView.setUserTrackingMode(.follow,animated:true)

make sure you add yourMKMapView.showsUserLocation = true in viewDidLoad()

确保在viewDidLoad()中添加你的MKMapView.showsUserLocation = true


推荐阅读
author-avatar
这辈子1015
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有