2017-02-14 28 views
14

Próbuję pokazać trzy rzeczy na mapie: GPS (obecna lokalizacja), Marker 1, Marker 2, ale nie jestem pewien, czy robię to dobrze, czy nie! Tu jest mój kodu:Google Maps pomniejsz GPS i znaczniki

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! 

więcej Kod:

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

Rezultatem jest tak:

it show only the start pin

Co potrzebne:

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

}

+0

Dodałem odpowiedź sprawdź to, upewnij się, że * didUpdateLocations() * jest wywoływana. –

Odpowiedz

2

można spróbować poniższy kod, to jest konwertowany z kodu objc tutaj jest dokumentacja 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)) 
+0

@ Mc.Lover czy to rozwiązało twój problem? –

+0

Nie działa też! –

10

Pokazuje wszystkie znaczniki z oprawionego ekranie :

Ją Próbuję podać ci jeden prosty przykład, mam nadzieję, że ci to pomoże. Za pomocą tego można wyświetlić dowolną liczbę znaczników na ekranie.

Przykład:

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

Czy mógłbyś mi powiedzieć, jak mam wypełnić 'tablica.count'? –

+0

@ Mc.Lover Zobacz, jeśli masz różne lat & long i jeśli dostajesz je z backendu, możesz użyć tego bezpośrednio lub stworzyć własną tablicę, dodając swój long-long i używając tej tablicy. –

+0

Dziękujemy! ale nadal nie działa! czy mógłbyś sprawdzić mój zredagowany kod? –

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

Uwaga:

locationArray zawiera trzy lokalizacje tj. Marker 1, Marker 2, lokalizacja użytkownika.