import UIKit
import CoreLocation
import GoogleMaps
class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, CLLocationManagerDelegate {
@IBOutlet weak var containView: UIView!
let locManager=CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locManager.delegate = self
self.locManager.desiredAccuracy = kCLLocationAccuracyBest
self.locManager.requestWhenInUseAuthorization()
self.locManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locManager.stopUpdatingLocation()
}
func showCurrentLocationOnMap() {
let camera = GMSCameraPosition.camera(withLatitude: (self.locManager.location?.coordinate.latitude)!, longitude: (self.locManager.location?.coordinate.latitude)!, zoom: 140)
let mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
mapView.isMyLocationEnabled = true
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Current location"
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = mapView
self.containView.addSubview(mapView)
}
我在我的控制器中添加了此代码,因为我试图在我当前位置使用标记显示地图(谷歌地图)但是没有工作(地图未显示),我在我的错误中做错了码?我该如何解决?
答案 0 :(得分:0)
我建议您在界面构建器上设置containView
类型,如此,也将名称更改为mapView
(要清楚),请注意类型GMSMapView
和将其链接到您的CourseClass2
控制器:
(我不在Mac上,所以我使用了这张图片。图片来源:http://vikrambahl.com/google-maps-ios-xcode-storyboards/)
尝试使用以下代码,至少应显示地图(不带标记)。
import UIKit
import CoreLocation
import GoogleMaps
class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: GMSMapView!
let locManager=CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locManager.delegate = self
self.locManager.desiredAccuracy = kCLLocationAccuracyBest
self.locManager.requestWhenInUseAuthorization()
self.locManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locManager.stopUpdatingLocation()
}
func showCurrentLocationOnMap() {
let camera = GMSCameraPosition.camera(withLatitude: (self.locManager.location?.coordinate.latitude)!, longitude: (self.locManager.location?.coordinate.latitude)!, zoom: 140)
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Current location"
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = mapView
}
还要小心强制解包选项(self.locManager.location?.coordinate.latitude)!
因为它可能会导致致命错误
答案 1 :(得分:0)
Xcode 9.2&斯威夫特4
转到info.plist文件,根据您的代码添加以下密钥。
func showCurrentLocationOnMap() {
let camera = GMSCameraPosition.camera(withLatitude: (self.locManager.location?.coordinate.latitude)!, longitude: (self.locManager.location?.coordinate.latitude)!, zoom: 10.0)
let mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
mapView.isMyLocationEnabled = true
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Current location"
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = mapView
self.view = mapView
}
移动到View控制器并用此代码替换showCurrentLocationOnMap()函数。
public class CustomTokenSerializer : WSSecurityTokenSerializer {
public CustomTokenSerializer(SecurityVersion sv)
: base(sv) { }
protected override void WriteTokenCore(XmlWriter writer, SecurityToken token) {
var userToken = token as UserNameSecurityToken;
var tokennamespace = "o";
var nonce = GetSHA1String(Guid.NewGuid().ToString());
writer.WriteRaw(
$@"<{tokennamespace}:UsernameToken u:Id=""{token.Id}"" xmlns:u=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"">
<{tokennamespace}:Username>{userToken.UserName}</{tokennamespace}:Username>
<{tokennamespace}:Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\"">{userToken.Password}</{tokennamespace}:Password>
<{tokennamespace}:Nonce EncodingType=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\"">{nonce}</{tokennamespace}:Nonce>
</{tokennamespace}:UsernameToken>"
);
}
protected string GetSHA1String(string phrase) {
SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider();
byte[] hashedDataBytes = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(phrase));
return Convert.ToBase64String(hashedDataBytes);
}
}