2017-02-14 19 views
14

Tôi đang cố gắng hiển thị ba điều trên bản đồ: GPS (vị trí hiện tại), Marker 1, Marker 2, nhưng tôi không chắc mình đang làm đúng hay không! Đây là mã của tôi:Google Maps thu nhỏ GPS và điểm đánh dấu

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! 

thêm Mã:

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

Kết quả là như thế này:

it show only the start pin

Những gì tôi cần:

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

Tôi đã thêm câu trả lời kiểm tra xem nó ra, đảm bảo * didUpdateLocations() * được gọi. –

Trả lời

2

Bạn có thể thử các mã dưới đây, điều này được chuyển đổi từ ObjC mã ở đây là tài liệu của 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 đã giải quyết được sự cố của bạn? –

+0

Không hoạt động! –

10

Hiển thị tất cả các dấu với màn hình ràng buộc :

Cô ấy e Tôi đang cố gắng cung cấp cho bạn một ví dụ đơn giản, hy vọng điều này sẽ giúp bạn. Sử dụng tính năng này, bạn có thể hiển thị bất kỳ số điểm đánh dấu nào trên màn hình.

Ví dụ:

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

bạn vui lòng cho tôi biết tôi nên điền vào 'array.count' như thế nào? –

+0

@ Mc.Lover Xem, nếu bạn có nhiều lat & dài và nếu bạn nhận được nó từ backend sau đó bạn có thể sử dụng một cách trực tiếp hoặc người nào khác tạo mảng của riêng bạn bằng cách thêm thời gian chờ của bạn và sử dụng mảng đó. –

+0

Cảm ơn bạn! nhưng vẫn không hoạt động! bạn vui lòng kiểm tra mã đã chỉnh sửa của tôi chứ? –

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

Lưu ý:

locationArray chứa ba địa điểm tức. Điểm đánh dấu 1, Điểm đánh dấu 2, vị trí của người dùng.

Các vấn đề liên quan