2017-05-15 6 views
0

Googleマップがあり、ユーザーの場所(都市など)をスニペットに表示したい。 どうすればいいですか?スニペットでユーザーの場所を表示する方法Googleマップスウィフト

ここに私の現在のコードです:

class ViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate{ 

@IBOutlet weak var mapView: GMSMapView! 

var latitude = -7.034323799999999 
var longitude = 110.42400399999997 

var locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 
    mapView.delegate = self 
    let camera = GMSCameraPosition.camera(withLatitude: Double(latitude), longitude: Double(longitude), zoom: 17) 
    mapView.animate(to: camera) 

    let markerImage = UIImage(named: "ic_home_detail_marker_location") 
    let markerView = UIImageView(image: markerImage) 

    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2DMake(Double(latitude), Double(longitude)) 
    marker.isDraggable = true 
    marker.snippet = "\(marker.position)" 
    mapView.selectedMarker = marker 
    marker.iconView = markerView 
    mapView.selectedMarker = marker 
    marker.map = mapView 
} 

}

+0

あなたはリンゴのジオコードブロックを呼び出す必要があります。 https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html –

答えて

0

ユーザーの都市や州名を取得したい場合は、CLGeocoderを使用する必要がありますが。

var currentLatitude:Double! 

var currentLongitude:Double! 

var cityName:String! 

var stateName:String! 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    print("locationManager function called") 

    // Fetch current location coordinates 

    let locValue:CLLocationCoordinate2D = (locationManager.location?.coordinate)! 

    currentLatitude = locValue.latitude 

    currentLongitude = locValue.longitude 

    print("Current Location = \(currentLatitude!), \(currentLongitude!)") 

    // Zoom to current location 

    let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: currentLatitude!, longitude: currentLongitude!, zoom: 17.0) 

    viewMap.camera = camera 

    // check for current city 

    CLGeocoder().reverseGeocodeLocation(locationManager.location!, completionHandler: {(placemarks, error) -> Void in 

     if error != nil { 

      print("Reverse geocoder failed with error" + (error?.localizedDescription)!) 

      return 
     } 

     if (placemarks?.count)! > 0 { 

      let pm = placemarks?[0] 

      self.cityName = (pm?.locality)! 

      self.stateName = (pm?.administrativeArea) 

      print("Current City: \(self.cityName!)") 

      print("Curret State: \(self.stateName!)") 

     } 
     else { 
      print("Problem with the data received from geocoder") 
     } 
    }) 

    locationManager.stopUpdatingLocation() 

} 

今では、変数に現在の都市が格納されています。

次のステップでは、ユーザーがマーカーに触れると、都市名が表示されます。

このデリゲートを追加する必要があります:

GMSMapViewDelegate 

を、ユーザーがそれをタップすると、これは、マーカー機能で、これが実現するためには、これを実装。

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { 

    mapView.delegate = self 
    marker.snippet = ("Current city: \(cityName!)") 

    // return false so as to show the marker details or 
    // return true to hide marker details. 

    return false 
} 
関連する問題