0

I am using the iOS Google Maps API to show a map. I want to add a line at a particular longitude so that users can know where that longitude is.

This is what I have tried:

let path = GMSMutablePath()
path.add(CLLocationCoordinate2D(latitude: -90, longitude: -122))
path.add(CLLocationCoordinate2D(latitude: 90, longitude: -122))
polyline = GMSPolyline(path: path)
polyline.geodesic = true
polyline.strokeColor = .red
polyline.strokeWidth = 20 // I have set this to a big number to guarantee that my poor eyesight can see the line
polyline.map = mapView

I cannot see the line anywhere!

As a control experiment, I copied the code that drew a rectangle from a tutorial and pasted it just after the above code:

let rect = GMSMutablePath()
rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))

// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.2);
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView

And it shows the rectangle:

enter image description here

Since my code that draws the line is immediately above the code that draws the rectangle, my code should have been executed as well. It should have drawn a line touching the rectangle, extending from the north pole to the south pole. But why is there no such line?

ielyamani
  • 17,807
  • 10
  • 55
  • 90
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • https://stackoverflow.com/questions/49530324/show-path-on-googlemaps-in-swift4 have a look on this – Wings Dec 01 '18 at 11:36
  • @wings I don't think that helps a lot because the OP of that post seems to be drawing a polyline with multiple segments. As you can see, I succeeded in drawing a rectangle. I just don't know how to draw a polyline with only two points. – Sweeper Dec 01 '18 at 11:40
  • What you actually want is to draw the Polyline between between to points and not to show the rectangle? – Wings Dec 01 '18 at 11:47
  • @wings Yes, I just want a geodesic line connecting the north pole and south pole, going through a certain longitude. I drew the rectangle just to test if Google Map itself is working. – Sweeper Dec 01 '18 at 11:48
  • then you can use that answer It will draw the polyline between your two points only It worked on because I am the OP or I can show you my code – Wings Dec 01 '18 at 11:51
  • @wings I only want a polyline joining two points (i.e. calling `add` twice). When I `add` a third point, the line shows up. But when I only use two points, the line does not show up. Any ideas why? – Sweeper Dec 01 '18 at 12:00
  • I don't know much about this Sir but you can check this https://stackoverflow.com/questions/44282454/draw-polyline-using-google-maps-in-custom-view-with-swift-3 it uses only two points – Wings Dec 01 '18 at 12:15
  • Points (90, X) and (-90, X) are the north and south poles no matter the value of X, and there are infinite lines that connect them. I'd suggest adding the point on the equator (0, X). – vacawama Dec 01 '18 at 12:40
  • @vacawama Oh so that's why! Thank you! – Sweeper Dec 01 '18 at 12:44
  • @vacawama I added a point on the equator between the north pole and south pole and now it only drew a line to the equator. – Sweeper Dec 01 '18 at 12:50
  • @Sweeper, did you place them in order? pole - equator - pole? – vacawama Dec 01 '18 at 12:51
  • @vacawama Yes: (-90, -122), (0, -122), (90 -122). – Sweeper Dec 01 '18 at 12:54
  • https://stackoverflow.com/q/42620510/5461400 try this – Harshal Valanda Dec 01 '18 at 13:06
  • I can't explain that. Seems like a bug. Do 89 and -89 work? How about 89.999 and -89.999? – vacawama Dec 01 '18 at 13:12
  • @vacawama 89 and -89 works, for some reason. And the line extends all the way to the borders of the map, so it's basically what I want. Thank you. – Sweeper Dec 01 '18 at 13:15

1 Answers1

1

The problem is that your two points aren't just two arbitrary points and there isn't just one shortest line between them.

Points (90, X) and (-90, X) are the north and south poles no matter the value of X, and there are infinite lines that connect them. I'd suggest adding the point on the equator (0, X) between the other two points.

I added a point on the equator between the north pole and south pole and now it only drew a line to the equator.

I can't explain that. Seems like a bug. Try values near the poles such as 89 and -89 or 89.999 and -89.999.

vacawama
  • 150,663
  • 30
  • 266
  • 294