I am using MKMapView and PolyLine concept to draw a line.But when the line is drawn line is getting thicker when it goes ahead.I want a single line throughout the route.
My code:-
func assigArray() {
if self.lat.count == self.lon.count {
for i in 0 ..< self.lat.count {
let destination = CLLocationCoordinate2DMake(self.lat[i], self.lon[i])
coordinateArray.append(destination)
}
self.mapp()
}
}
func mapp() {
let coords2 = CLLocationCoordinate2D(latitude: lat.last!, longitude: lon.last!)
let testline = MKPolyline(coordinates: coordinateArray, count: coordinateArray.count)
//Add `MKPolyLine` as an overlay.
map.add(testline)
map.delegate = self
map.centerCoordinate = coords2
map.region = MKCoordinateRegion(center: coords2, span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02))
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 2.0
return renderer
}
Related
I've been trying to figure this one out with no luck.
What I can do:
Show user's current location
Show a pin wherever I want (depending on lat and long)
What I can't figure out:
How do I create a geofenced circle around this location?
func setupData() {
// 1. check if system can monitor regions
if CLLocationManager.isMonitoringAvailable(for:
CLCircularRegion.self) {
// 2. region data
let title = "Marina Bar Hop"
let coordinate = CLLocationCoordinate2DMake(33.97823607957177, -118.43823725357653)
let regionRadius = 300.0
// 3. setup region
let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude,
longitude: coordinate.longitude), radius: regionRadius, identifier: title)
locationManager.startMonitoring(for: region)
// 4. setup annotation
let restaurantAnnotation = MKPointAnnotation()
restaurantAnnotation.coordinate = coordinate;
restaurantAnnotation.title = "\(title)";
mapView.addAnnotation(restaurantAnnotation)
// 5. setup circle
let circle = MKCircle(center: coordinate, radius: regionRadius)
mapView.addOverlay(circle)
}
else {
print("System can't track regions")
}
}
// 6. draw circle
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.strokeColor = UIColor.red
circleRenderer.lineWidth = 1.0
return circleRenderer
}
See "draw circle" and "set up circle" ^
In case there's an error I'm not seeing, the rest of the code is here.
Any help is greatly appreciated! I thought I've done everything to solve this issue, but it still won't work. :( Thank you in advance!
The trouble with this question is that the code in the question does not represent faithfully the real code. Fortunately, you also posted the real code:
https://github.com/kcapretta/RadiusSocialNetworkingApp/blob/master/RadiusMap/RadiusLocationViewController
In that version of the code, we see clearly that your delegate method
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay)
-> MKOverlayRenderer {
is buried inside your viewDidLoad. Thus it is purely a local function, invisible to all other code. You need to get it out of there so that it is a method of the view controller.
To demonstrate, here's a slight variant on your code, leaving out the unnecessary stuff about the region monitoring and the user's current location and concentrating just on the annotation and the circle overlay:
class ViewController: UIViewController, MKMapViewDelegate {
#IBOutlet var mapView : MKMapView!
let coordinate = CLLocationCoordinate2DMake(33.97823607957177, -118.43823725357653)
// this is a _method_
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.region = MKCoordinateRegion(center: coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)
let title = "Marina Bar Hop"
let restaurantAnnotation = MKPointAnnotation()
restaurantAnnotation.coordinate = coordinate
restaurantAnnotation.title = title
mapView.addAnnotation(restaurantAnnotation)
let regionRadius = 300.0
let circle = MKCircle(center: coordinate, radius: regionRadius)
mapView.addOverlay(circle)
}
// this is a _method_
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.strokeColor = UIColor.red
circleRenderer.lineWidth = 1.0
return circleRenderer
}
}
The result:
I created seven different polylines. However some of them are disappearing when I zoom in closely. Why it is happening? How can I prevent this?
Here is my polyline renderer:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.9)
renderer.lineWidth = 2.2
return renderer
}
//Thousands of parameters sending as a parameter
func createPathWithPoints(_ points: [MKMapPoint]) {
let arc = MKPolyline(points: points, count: points.count)
mapView.addOverlay(arc)
}
Please help!
I had similar problem with using MKPolyline and below is what I did to fix this.
1) Make sure that you have your mapView delegate in viewDidLoad().
mapView.delegate = self
2) Add your overlay to the map.
mapView.addOverlay(polyLine())
I am using coredata in my project, so if myLocations are empty then I return empty MKPolyline()
private func polyLine() -> MKPolyline {
guard let locations = myLocations else {
return MKPolyline()
}
// Coordinates
let coords: [CLLocationCoordinate2D] = locations.map { location in
let location = location as! Location
return CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
}
return MKPolyline(coordinates: coords, count: coords.count)
}
3) We have access to rendererFor from MKMapViewDelegate. You can change color and width for polyline.
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
guard let polyline = overlay as? MKPolyline else {
return MKOverlayRenderer(overlay: overlay)
}
// Setup renderer
let renderer = MKPolylineRenderer(polyline: polyline)
renderer.strokeColor = .systemBlue
renderer.lineWidth = 3
return renderer
}
I am trying to draw a MKCircle around a map annotation. I think the code is right so far but not sure why it isn't working. I believe I have all the code needed for it to work.
func getPlaces(){
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("Businesses").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
// print("\(snap.value)")
if let locationDict = snapshot.value as? [String:AnyObject]{
let lat = Double(locationDict["businessLatitude"] as! String)
let long = Double(locationDict["businessLongitude"] as! String)
let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
let radius = 100.0
self.mapView!.setRegion(region, animated: true)
let circle = MKCircle(center: center, radius: radius)
let annotation = MKPointAnnotation()
annotation.coordinate = region.center
self.mapView.addAnnotation(annotation)
self.mapView.add(circle)
}
})
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.strokeColor = UIColor.red
circleRenderer.lineWidth = 1.0
return circleRenderer
}
Did you set the mapview delegate?
self.mapView.delegate = self
Don't forget MKMapViewDelegate protocol.
class ViewController: UIViewController, MKMapViewDelegate {
...
}
I'm using Xcode 8.3.2 so first I import mapkit. Then I set markers to the map. Then I add the following code to add a polyline to the map but it won't show any.
class ViewController: UIViewController, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
self.mapView.delegate = self
super.viewDidLoad()
let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
let point1 = CLLocationCoordinate2D(latitude: 6.9271, longitude: 79.8612);
let point2 = CLLocationCoordinate2D(latitude: 9.6615, longitude: 80.0255);
let overlay = MKTileOverlay(urlTemplate: template)
overlay.canReplaceMapContent = true
let location = CLLocationCoordinate2DMake(6.878069, 79.892119)
mapView.add(overlay, level: .aboveLabels)
mapView.setRegion(MKCoordinateRegionMakeWithDistance(location, 1100, 1100), animated: true)
let pin = PinAnnotation(title: "Nimbus", subtitle: "Best", coordinate: location)
mapView.addAnnotation(pin)
let points: [CLLocationCoordinate2D]
points = [point1, point2]
let polyline = MKGeodesicPolyline(coordinates: points, count: 3)
mapView.add(polyline)
UIView.animate(withDuration: 1.5, animations: { () -> Void in
let span = MKCoordinateSpanMake(0.01, 0.01)
let region1 = MKCoordinateRegion(center: point1, span: span)
self.mapView.setRegion(region1, animated: true)
})
}
func mapView(_ mapview: MKMapView, rendererFor overlay: MKOverlay) ->MKOverlayRenderer{
if let overlayGeodesic = overlay as? MKGeodesicPolyline
{
let overLayRenderer = MKPolylineRenderer(polyline: overlayGeodesic)
overLayRenderer.lineWidth = 5
overLayRenderer.strokeColor = UIColor.blue
return overLayRenderer
}
return MKOverlayRenderer(overlay: overlay)
}
First you need to add this line, I think you already have added but anyway
self.mapView.delegate = self
After that you need to implement this MKMapViewDelegate method func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer and return the MKOverlayRenderer needed for your current overlay in this case MKPolylineRenderer this is an important part if you don't implement this method then you never will have your polyline rendered
implementation will be something like this
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let overlayGeodesic = overlay as? MKGeodesicPolyline
{
let overLayRenderer = MKPolylineRenderer(polyline: overlayGeodesic)
overLayRenderer.lineWidth = 5
overLayRenderer.strokeColor = UIColor.blue
return overLayRenderer
}
if let overlayTile = overlay as? MKTileOverlay{
let overLayRenderer = MKTileOverlayRenderer(tileOverlay: overlayTile)
return overLayRenderer
}
return MKOverlayRenderer(overlay: overlay)
}
And voila! there is your polyLine rendered
I have to draw an arc having two angles, center point and radius as input. I use UIBezierPath, but overlay isn't added to the map. Here's my code:
func calculateByArc()
{
let startingAzimuth = Double(upperLimitTextBox.text!)
let endingAzimuth = Double(upperLimitUomTextBox.text!)
let radius = Double(lowerLimitTextBox.text!)
let latitude = Double(lowerLimitUomTextBox.text!)
let longitude = Double(textField5.text!)
let clockwise = clockwiseSwitch.isOn ? true : false
let qwe = CLLocationCoordinate2DMake(latitude!,longitude!)
let asd = Map.convert(qwe, toPointTo: Map)
let test = Map.convert(asd, toCoordinateFrom: Map)
print("test lat: " + String(test.latitude))
print("test lon: " + String(test.longitude))
let path = UIBezierPath(arcCenter: Map.convert (qwe, toPointTo: mapController.Map), radius: CGFloat(radius!), startAngle: CGFloat(Helper.toRad(startingAzimuth!)), endAngle: CGFloat(Helper.toRad(endingAzimuth!)) , clockwise: clockwise)
let arc = MKCircle(center: CLLocationCoordinate2DMake(latitude!, longitude!), radius: radius!)
arc.accessibilityPath = path
Map.add(arc)
}
and my mapView:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
{
if overlay is MKCircle
{
print("overlay latitude: "+String(overlay.coordinate.latitude))
print("overlay longitude: "+String(overlay.coordinate.longitude))
let circleOverlay = overlay as! MKCircle
if(circleOverlay.accessibilityPath != nil)
{
let arcRenderer = MKOverlayPathRenderer()
arcRenderer.path = circleOverlay.accessibilityPath?.cgPath
arcRenderer.strokeColor = UIColor.red
arcRenderer.lineWidth = 10
arcRenderer.alpha = 0.3
return arcRenderer
}
let circle = MKCircleRenderer(overlay: overlay)
circle.strokeColor = UIColor.black
circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
circle.lineWidth = 1
circle.alpha = 0.3
return circle
}
}
I guess it's because i don't properly convert CLLocationCoordinate2D to CGPoint, because a couple of weeks ago i managed to draw an arc, but with wrong coordinates(can't remember how i did that).
Is this what you are looking for http://nshipster.com/mkgeodesicpolyline/ ?