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

MapKit缩放到用户当前位置-MapKitzoomtousercurrentlocation

Iamtryingtosimplyshowuserslocationonthemap,butIneedtowhenapplaunches,themapshou

I am trying to simply show user's location on the map, but I need to when app launches, the map should zoom to current location ,but I don't know why map doesn't zoom at all and it's like this :

我试图简单地在地图上显示用户的位置,但是我需要在应用程序启动时,地图应该缩放到当前位置,但我不知道为什么地图根本不会缩放,它是这样的:

#4


1  

when you set region -> you cannot zoom the map anymore. below to fix that

当你设置区域 - >你不能再缩放地图。下面来解决这个问题

func yourFuncName() {
//this is global var
regiOnHasBeenCentered= false
if !self.regionHasBeenCentered {
    let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
    let userLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(_cllocationOfUserCurrentLocation!.coordinate.latitude, _cllocationOfUserCurrentLocation!.coordinate.longitude)
    let region: MKCoordinateRegion = MKCoordinateRegionMake(userLocation, span)

    self.mapView.setRegion(region, animated: true)
    self.regiOnHasBeenCentered= true
    }

    self.mapView.showsUserLocation = true
}

#5


0  

Try with MKMapViewDelegate func:

尝试使用MKMapViewDelegate func:

var isInitiallyZoomedToUserLocation: Bool = false 

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    if !isInitiallyZoomedToUserLocation {
       isInitiallyZoomedToUserLocation = true
       mapView.showAnnotations([userLocation], animated: true)
    }
}

#6


0  

In swift 4.1. To change the Zoom level you need to change the span value i.e MKCoordinateSpan(latitudeDelta: 0.95, longitudeDelta: 0.95)

在swift 4.1中。要更改缩放级别,您需要更改跨度值,即MKCoordinateSpan(latitudeDelta:0.95,longitudeDelta:0.95)

let lat =  "33.847105"
let lOng= "-118.2673272"
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(long)!), span: MKCoordinateSpan(latitudeDelta: 0.95, longitudeDelta: 0.95))
DispatchQueue.main.async {
    self.mapView(self.mapView, regionDidChangeAnimated: true)
}

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