Google Maps уменьшает масштаб до GPS и маркеров

Я пытаюсь показать три вещи на карте: GPS (текущее местоположение), маркер 1, маркер 2, но я не уверен, что я делаю это правильно или нет! Вот мой код:

 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!

Подробнее Код:

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

Результат выглядит следующим образом:

он показывает только начальный вывод

Что мне нужно:

введите описание изображения здесь

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

}

Ответы

Ответ 1

Отображение всех маркеров с привязкой к экрану:

Здесь я пытаюсь предоставить вам один простой пример, надеюсь, что это вам поможет. Используя это, вы можете показать любое количество маркеров на экране.

Пример:

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

Ответ 2

Можете ли вы попробовать приведенный ниже код, это преобразованный из кода ObjC, вот документация includingCoordinate

let bounds = GMSCoordinateBounds()
bounds.includingCoordinate(self.startMarker.position)
bounds.includingCoordinate(self.endMarker.position)
bounds.includingCoordinate(yourCurrentLocationPosition)
self.mapView.animateWithCameraUpdate(GMSCameraUpdate(fitBounds:bounds, padding:20.0f))

Ответ 3

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    showMarkers(locations)
    //stop updating location when you find suitable
}

func showMarkers(userLocation: [CLLocation]){

    let location1: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: self.startLatitude, longitude: self.startLongitude)
    let location2: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: self.endLatitude, longitude: self.endLongitude)
    let location3: CLLocationCoordinate2D = userLocation[0].coordinate

    var locationArray = []
    locationArray.append(location1)
    locationArray.append(location2)
    locationArray.append(location3)

    var bounds = GMSCoordinateBounds()
    for location in locationArray
    {
        let latitude = location.valueForKey("latitude")
        let longitude = location.valueForKey("longitude")

        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        marker.map = self.mapView
        bounds = bounds.includingCoordinate(marker.position)
    }
    let update = GMSCameraUpdate.fitBounds(bounds, withPadding: 100)
    mapView.animateWithCameraUpdate(update)
}

Примечание:

locationArray содержит три местоположения, т.е. Маркер 1, Маркер 2, местоположение пользователя.

Ответ 4

Для тех, кто ищет решение с Objective-C, это может помочь

//Locations
CLLocationCoordinate2D source = CLLocationCoordinate2DMake(19.2880, 72.1587);
CLLocationCoordinate2D userLocation = CLLocationCoordinate2DMake(19.1780, 72.9577);
CLLocationCoordinate2D destination = CLLocationCoordinate2DMake(19.0760, 72.8777);

//Create a GMSMutablePath
GMSMutablePath *path = [GMSMutablePath new];
[path addCoordinate:source];
[path addCoordinate:userLocation];
[path addCoordinate:destination];

//Create bounds
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc]initWithPath:path];

//Update the camera position
[mapview animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]];