Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps zoom out to GPS and markers

I am trying to show three things on map : GPS (the current location) , Marker 1 , Marker 2, but I am not sure I am doing it right or not ! Here is my code :

 self.startMarker.position = CLLocationCoordinate2D(latitude: self.startLatitude, longitude: self.startLongitude)
 self.startMarker.icon = #imageLiteral(resourceName: "Pin Start")
 self.startMarker.map = self.mapView


 self.endMarker.position = CLLocationCoordinate2D(latitude: self.endLatitude, longitude: self.endLongitude)
 self.endMarker.icon = #imageLiteral(resourceName: "Pin End")
 self.endMarker.map = self.mapView


 let southWest = CLLocationCoordinate2DMake(self.startLatitude,self.startLongitude)
 let northEast = CLLocationCoordinate2DMake(self.endLatitude,self.endLongitude)
 let bounds = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
 let camera = self.mapView.camera(for: bounds, insets:.zero)
 self.mapView.camera = camera!

More Code :

  // MARK: - Google Map

private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
            if status == CLAuthorizationStatus.authorizedWhenInUse {
                mapView.isMyLocationEnabled = true
            }
          }
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let newLocation = locations.last
            mapView.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 14)
            mapView.isMyLocationEnabled = true
        }

The result is like this :

it show only the start pin

What I need :

enter image description here

EDITED

if let myLocation = self.mapView.myLocation {

let path = GMSMutablePath()
path.add(myLocation.coordinate)
path.add(self.startMarker.position)
path.add(self.endMarker.position)

let bounds = GMSCoordinateBounds(path: path)
                                        self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 40))

}

like image 956
iOS.Lover Avatar asked Feb 14 '17 08:02

iOS.Lover


People also ask

How do I zoom in and out on a map?

The shortcut is simple: just double-tap the maps interface, but instead of lifting your finger after the second tap (the shortcut for zooming in), you leave it touching the screen. Then a swipe up zooms out, and a swipe down zooms in.

How do I change the default zoom on Google Maps?

Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.


1 Answers

Showing All Markers with screen bound:

Here I am trying to provide you one simple example, hope this will help you. Using this, you can show any number of markers on screen.

Example:

let path = GMSMutablePath()
for var i in 0 ..< array.count //Here take your "array" which contains lat and long.
{
    let marker = GMSMarker()
    let lat = Double(array.objectAtIndex(i).valueForKey(lattitude) as! String)
    let long = Double(arrayr.objectAtIndex(i).valueForKey(longitude) as! String)      
    marker.position = CLLocationCoordinate2DMake(lat!,long!)
    path.addCoordinate(marker.position)
    marker.map = self.mapView
}
let bounds = GMSCoordinateBounds(path: path)
self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 30.0))
like image 142
Sagar Sukode Avatar answered Oct 10 '22 22:10

Sagar Sukode