2017-02-14 11 views
14

मैं मानचित्र पर तीन चीजें दिखाने की कोशिश कर रहा हूं: जीपीएस (वर्तमान स्थान), मार्कर 1, मार्कर 2, लेकिन मुझे यकीन नहीं है कि मैं इसे सही कर रहा हूं या नहीं! यहाँ मेरी कोड है:Google मानचित्र जीपीएस और मार्करों के लिए ज़ूम आउट

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 
     } 

परिणाम इस तरह है:

it show only the start pin

मैं क्या जरूरत है:

enter image description here

संपादित

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

मैंने एक उत्तर जोड़ा है, इसे जांचें, सुनिश्चित करें कि * didUpdateLocations() * कहा जाता है। –

उत्तर

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

@ एमसीएलओवर ने आपकी समस्या का समाधान किया? –

+0

या तो काम नहीं करता है! –

10

के प्रलेखन स्क्रीन बाध्य के साथ सभी मार्करों दिखा है :

उसका और मैं आपको एक साधारण उदाहरण प्रदान करने की कोशिश कर रहा हूं, उम्मीद है कि यह आपकी मदद करेगा। इसका उपयोग करके, आप स्क्रीन पर मार्करों की संख्या दिखा सकते हैं।

उदाहरण:

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

क्या आप कृपया मुझे बताएंगे कि मुझे 'array.count' कैसे भरना चाहिए? –

+0

@ एमसीएलओवर देखें, यदि आपके पास विभिन्न अक्षांश और लंबे समय हैं और यदि आप इसे बैकएंड से प्राप्त कर रहे हैं तो आप उस का उपयोग सीधे कर सकते हैं या फिर अपने लेट लम्बे जोड़कर और उस सरणी का उपयोग करके अपना स्वयं का सरणी बना सकते हैं। –

+0

धन्यवाद! लेकिन अभी भी काम नहीं करता है! क्या आप कृपया मेरा संपादित कोड देखेंगे? –

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

नोट:

locationArray तीन स्थानों यानी शामिल हैं। मार्कर 1, मार्कर 2, उपयोगकर्ता का स्थान

संबंधित मुद्दे