【问题标题】:Create Map Image through Latitude and Longitude in ios swift4在ios swift4中通过纬度和经度创建地图图像
【发布时间】:2020-09-23 19:03:38
【问题描述】:

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

【问题讨论】:

  • 您的问题似乎不清楚,所以请您以更好的方式解释它,例如您想要什么,如果您已经完成了任何类型的代码,那么您需要帮助然后证明该代码在哪个问题更适合我们帮助您
  • 我想在图像视图上显示地图,我已经知道位置的纬度和经度了。以编程方式需要这些。

标签: swift google-maps latitude-longitude


【解决方案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】:

    不知道你有没有看文档,我的解决方案是这样的

    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
    

    【讨论】:

    • 然后可以截图mapViewhere
    【解决方案3】:

    不要捕获地图的快照然后将其显示在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)
    

    【讨论】:

      【解决方案4】:
      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)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-21
        • 1970-01-01
        • 2015-08-03
        相关资源
        最近更新 更多