在 ios swift4 中通过经纬度创建地图图像
Create Map Image through Latitude and Longitude in ios swift4
通过 API 我有特定位置的纬度和经度,需要在图像视图上以地图形式显示这些坐标。
我想通过这些坐标绘制地图视图图像。
想要显示特定位置以及经纬度。
我怎样才能做到?
不知道你有没有看文档,我的解决方法是这样的
func zoomToLocation(with coordinate: CLLocationCoordinate2D) {
//You can change the meters as you wish
let region = MKCoordinateRegion(center: coordinate, latitudinalMeters: 5000, longitudinalMeters: 5000)
map.setRegion(region, animated: true)
}
并且您可以使用此代码锁定缩放
map.isZoomEnabled = false
不是捕获地图快照然后在 imageview
中显示它,而是将 mapview
放在那里。在地图上设置您想要的位置。并禁用 mapview
.
的用户交互
下面是相关代码
let location = GMSCameraPosition.camera(withLatitude: yourLatitude, longitude: yourLongitude, zoom: 17.0) // Set zoom level according to your requirement
mapView.animate(to: location)
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locationUpdate == false {
locationUpdate = true
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
var latStr = ""
var longStr = ""
latStr = String(locValue.latitude)
longStr = String(locValue.longitude)
let staticMapUrl = "http://maps.google.com/maps/api/staticmap?markers=color:blue|\(latStr),\(longStr)&\("zoom=10&size=400x300")&sensor=true&key=AIzaSyBXAdCe4nJuapECudMeh4q-gGlU-yAMQX0"
print(staticMapUrl)
let url = URL(string: staticMapUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
do {
let data = try NSData(contentsOf: url!, options: NSData.ReadingOptions())
imgLocationOnMap.image = UIImage(data: data as Data)
} catch {
imgLocationOnMap.image = UIImage()
}
}
}
func updateMap(_ coordinates : CLLocationCoordinate2D){
var mapRegion = MKCoordinateRegionMakeWithDistance(coordinates, 0, 0)
mapRegion.center = coordinates
cMap.setRegion(mapRegion, animated: true)
let artwork = Artwork(title: "",
locationName: "",
coordinate: coordinates)
cMap.removeAnnotations(cMap.annotations)
cMap.addAnnotation(artwork)
}
通过 API 我有特定位置的纬度和经度,需要在图像视图上以地图形式显示这些坐标。 我想通过这些坐标绘制地图视图图像。 想要显示特定位置以及经纬度。 我怎样才能做到?
不知道你有没有看文档,我的解决方法是这样的
func zoomToLocation(with coordinate: CLLocationCoordinate2D) {
//You can change the meters as you wish
let region = MKCoordinateRegion(center: coordinate, latitudinalMeters: 5000, longitudinalMeters: 5000)
map.setRegion(region, animated: true)
}
并且您可以使用此代码锁定缩放
map.isZoomEnabled = false
不是捕获地图快照然后在 imageview
中显示它,而是将 mapview
放在那里。在地图上设置您想要的位置。并禁用 mapview
.
下面是相关代码
let location = GMSCameraPosition.camera(withLatitude: yourLatitude, longitude: yourLongitude, zoom: 17.0) // Set zoom level according to your requirement
mapView.animate(to: location)
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locationUpdate == false {
locationUpdate = true
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
var latStr = ""
var longStr = ""
latStr = String(locValue.latitude)
longStr = String(locValue.longitude)
let staticMapUrl = "http://maps.google.com/maps/api/staticmap?markers=color:blue|\(latStr),\(longStr)&\("zoom=10&size=400x300")&sensor=true&key=AIzaSyBXAdCe4nJuapECudMeh4q-gGlU-yAMQX0"
print(staticMapUrl)
let url = URL(string: staticMapUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
do {
let data = try NSData(contentsOf: url!, options: NSData.ReadingOptions())
imgLocationOnMap.image = UIImage(data: data as Data)
} catch {
imgLocationOnMap.image = UIImage()
}
}
}
func updateMap(_ coordinates : CLLocationCoordinate2D){
var mapRegion = MKCoordinateRegionMakeWithDistance(coordinates, 0, 0)
mapRegion.center = coordinates
cMap.setRegion(mapRegion, animated: true)
let artwork = Artwork(title: "",
locationName: "",
coordinate: coordinates)
cMap.removeAnnotations(cMap.annotations)
cMap.addAnnotation(artwork)
}