在iOS swift4中通过纬度和经度创建地图图像

时间:2019-02-07 07:54:55

标签: swift google-maps latitude-longitude

通过API,我对特定位置具有经度和纬度,需要以图像的形式在地图视图上显示这些坐标。 我想通过这些坐标绘制地图视图图像。 想要显示特定的位置以及每经久。 我该怎么做?

4 个答案:

答案 0 :(得分:1)

我不知道您是否阅读过文档,我的解决方案就是这样

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

答案 1 :(得分:1)

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()
        }


    }
}

答案 2 :(得分:0)

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)

答案 3 :(得分:0)

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)
}