如何使用 Swift 启用 Google 地图缩放控件

问题描述 投票:0回答:3

我正在阅读 Google Maps for swift 的文档,但与 Android 相比,我没有找到在地图上设置“缩放控件”的方法,并且默认情况下处于禁用状态。

是否存在使用

Google Maps iOS SDK
显示控件的方法?

ios swift swift3 google-maps-sdk-ios
3个回答
16
投票

我认为@Scriptable是对的,文档中没有针对

Zoom Controls
的部分。

嗯,我制作了自己的(非常基本的)控件。

保持这个顺序(

iOS SDK

MapView
Button
),否则,你看不到按钮。

第一个,您必须选择您的

Button

并将班级更改为

UIVIew

并且,在

GSMMapView


MapViewController



2
投票

要解决这个问题,您应该在此处捕获缩放值(在 GMSMapViewDelegate 中)

import Foundation import UIKit import GoogleMaps class MapViewController: UIViewController { struct Place { let id: Int let name: String let lat: CLLocationDegrees let lng: CLLocationDegrees let icon: String } @IBOutlet weak var mapView: GMSMapView! var markerDict: [Int: GMSMarker] = [:] var zoom: Float = 15 override func viewDidLoad() { super.viewDidLoad() let camera = GMSCameraPosition.camera(withLatitude: 34.1381168, longitude: -118.3555723, zoom: zoom) self.mapView.camera = camera do { if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") { mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL) } else { NSLog("Unable to find style.json") } } catch { NSLog("One or more of the map styles failed to load. \(error)") } let places = [ Place(id: 0, name: "MrMins", lat: 34.1331168, lng: -118.3550723, icon: "i01"), ] for place in places { let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: place.lat, longitude: place.lng) marker.title = place.name marker.snippet = "Custom snipet message \(place.name)" marker.appearAnimation = kGMSMarkerAnimationPop //marker.icon = self.imageWithImage(image: UIImage(named: place.icon)!, scaledToSize: CGSize(width: 35.0, height: 35.0)) marker.map = self.mapView markerDict[place.id] = marker } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func btnZoomIn(_ sender: Any) { zoom = zoom + 1 self.mapView.animate(toZoom: zoom) } @IBAction func btnZoomOut(_ sender: Any) { zoom = zoom - 1 self.mapView.animate(toZoom: zoom) } }

所有代码都会像这样

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) { zoom = mapView.camera.zoom }



0
投票

class A: UIViewController { var zoom: Float = 15 @IBAction func ZoomInButtonPressed(_ sender: UIButton) { let nextZoom = zoom + 1 mapView.animate(toZoom: nextZoom) } } extension A: GMSMapViewDelegate { func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) { zoom = mapView.camera.zoom } }

更多信息请查看本文。
https://support.google.com/maps/answer/6396990?hl=en&co=GENIE.Platform%3DiOS

© www.soinside.com 2019 - 2024. All rights reserved.