1

Through API I have latitude and longitude for particular location, need to show those coordinate on image view as form of map. I want to draw map view image though these coordinate. Want to show particular location as well as per lat long. How can I make it?

Deviyani Swami
  • 749
  • 8
  • 17
  • your question doesn't seems to be clear so can you please explain it in better way like what exactly you want and if you have done any kind code then for which you need help then prove that code in the question which will be better for us to help you – Parth Dhorda Feb 07 '19 at 07:59
  • I want to show map on Image view, already I have lat and long for location. need these through programmatically. – Deviyani Swami Feb 08 '19 at 05:15

4 Answers4

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


    }
}
Deviyani Swami
  • 749
  • 8
  • 17
1

I Don't know if you read the docs, my solution was like this

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

And you can lock the zoom with this code

map.isZoomEnabled = false
Said Alır
  • 170
  • 2
  • 15
  • Then you can take a screenshot of mapView [here](https://stackoverflow.com/questions/30696307/how-to-convert-a-uiview-to-an-image) – ChanOnly123 Feb 07 '19 at 12:39
0

Instead of capturing a snapshot of the map and then showing it in imageview, put mapview there. Set your desired location on the map. And disable the user interaction of mapview.

Below is the relevant code.

let location = GMSCameraPosition.camera(withLatitude: yourLatitude, longitude: yourLongitude, zoom: 17.0) // Set zoom level according to your requirement
mapView.animate(to: location)
Shubham
  • 763
  • 5
  • 20
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)
}
David Bradshaw
  • 11,859
  • 3
  • 41
  • 70