Skip to content

Instantly share code, notes, and snippets.

@nphsu
Last active November 26, 2017 11:26
Show Gist options
  • Save nphsu/c140c571eb057ca2801ca82de38d61d3 to your computer and use it in GitHub Desktop.
Save nphsu/c140c571eb057ca2801ca82de38d61d3 to your computer and use it in GitHub Desktop.
【Swift】Google Map API for iOSを使用してみた ref: https://qiita.com/shunp/items/f8b03f3a35a3ee5b444f
import UIKit
import GoogleMaps
class DemoViewController:UIViewController {
override func loadView() {
let camera = GMSCameraPosition.camera(withLatitude:1.285, longitude:103.848, zoom:12)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
self.view = mapView
}
}
mapView.isMyLocationEnabled = true
if let mylocation = mapView.myLocation {
print("User's location: \(mylocation)")
} else {
print("User's location is unknown")
}
// Create a rectangular path
let rect = GMSMutablePath()
rect.add(CLLocationCoordinate2D(latitude:37.36, longitude: -122.0))
rect.add(CLLocationCoordinate2D(latitude:37.45, longitude: -122.0))
rect.add(CLLocationCoordinate2D(latitude:37.45, longitude: -122.2))
rect.add(CLLocationCoordinate2D(latitude:37.36, longitude: -122.2))
// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red:0.25, green:0, blue:0, alpha:0.05);
polygon.strokeColor = .black
polygon.strokeWidth = 2
polygon.map = mapView
// The code snippet below shows how to create and display a GMSPlacePickerViewController.
@IBAction func pickPlace(_ sender: UIButton) {
let config = GMSPlacePickerConfig(viewport: nil)
let placePicker = GMSPlacePickerViewController(config: config)
present(placePicker, animated: true, completion: nil)
}
// To receive the results from the place picker 'self' will need to conform to
// GMSPlacePickerViewControllerDelegate and implement this code.
func placePicker(_ viewController: GMSPlacePickerViewController, didPick place: GMSPlace) {
// Dismiss the place picker, as it cannot dismiss itself.
viewController.dismiss(animated: true, completion: nil)
print("Place name \(place.name)")
print("Place address \(place.formattedAddress)")
print("Place attributions \(place.attributions)")
}
func placePickerDidCancel(_ viewController: GMSPlacePickerViewController) {
// Dismiss the place picker, as it cannot dismiss itself.
viewController.dismiss(animated: true, completion: nil)
print("No place selected")
}
let mapInsets = UIEdgeInsets(top:100.0, left:0.0, bottom:0.0, right:300.0)
mapView.padding = mapInsets
import UIKit
import GoogleMaps
class ViewController:UIViewController, GMSMapViewDelegate {
...
override func loadView() {
let camera = GMSCameraPosition.camera(withLatitude:47.603,
longitude:-122.331,
zoom:14)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
mapView.delegate = self
self.view = mapView
}
func mapView(_ mapView:GMSMapView, didTapPOIWithPlaceID placeID:String,
name:String, location:CLLocationCoordinate2D) {
print("You tapped \(name): \(placeID), \(location.latitude)/\(location.longitude)")
}
}
import UIKit
import GoogleMaps
class ViewController:UIViewController, GMSMapViewDelegate {
override func loadView() {
let panoView = GMSPanoramaView(frame: .zero)
self.view = panoView
panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: -33.732, longitude:150.312))
}
}
marker.icon = GMSMarker.markerImage(with: .black)
let position = CLLocationCoordinate2D(latitude:51.5, longitude: -0.127)
let london = GMSMarker(position: position)
london.title = "London"
london.icon = UIImage(named: "house")
london.map = mapView
marker.opacity = 0.6
let position = CLLocationCoordinate2D(latitude:51.5, longitude: -0.127)
let london = GMSMarker(position: position)
london.title = "London"
london.snippet = "Population: 8,174,100"
london.map = mapView
marker.tracksInfoWindowChanges = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment