lifewithelliott lifewithelliott - 6 months ago 126x
Swift Question

Google Maps for iOS, swift - How to show entire polyline between markers?

I am trying to fit a polyline in the google map view. The polyline was acquired through overview_polyline in the google maps directions api.

Wondering how I would be able to convert an encoded polyline into something that can be worked with. I need to fit the polyline in the map view. All i have found out to do is fit the bounds to show all markers but not showcase the entire polyline.

func fitAllMarkers()

var bounds = GMSCoordinateBounds()

for marker in markers
bounds = bounds.includingCoordinate(marker.position)




Here is the method that may help to fit polyline in mapview.

fun yourMethod
    CGRect rect = CGRectMake(0,200,UIScreen.mainScreen().bounds.size.width,UIScreen.mainScreen().bounds.size.height-250)

    for (int i=20; i>0; i--) { 
      CGPoint sourcepoint = myMapView.projection.pointForCoordinate(source)
            CGPoint destinationpoint =  myMapView.projection.pointForCoordinate(destination)

       if (CGRectContainsPoint(rect,sourcepoint) && CGRectContainsPoint(rect,destinationpoint)) 
        GMSCameraPosition *camera = GMSCameraPosition.cameraWithLatitude((source.latitude,+,destination.latitude)/2,longitude:(source.longitude,+,destination.longitude)/2,zoom:i)



I have views top and bottom of mapview. so I've took origin.y to 200 and size.y to some value. Change it as per your requirement.


for (int i=20; i>0; i--)

I've took max_zoom to 20 (i.e., i=20).

Hope this helps.