谷歌地图GMSMarker没有出现在地图上
问题描述:
在Swift中,我下载了位置数据并在Google地图上设置了一个标记,但标记并未出现在地图上。谷歌地图GMSMarker没有出现在地图上
数据项:
init(id: Int?,name: String?, owner: String?, address: String?, lon: String!, lat: String!, phoneNum: String?) {
self.id = id
self.name = name
self.owner = owner
self.address = address
self.phoneNum = phoneNum
self.lon = Double(lon)
self.lat = Double(lat)
self.coordinate = CLLocationCoordinate2DMake(self.lat, self.lon)
}
设置标记(标记不会出现在地图上):
let data = UserData.shareInstance()
for item in data.itemsArray {
let marker = GMSMarker()
marker.position = item.coordinate
marker.title = item.name!
marker.map = mapView
}
但是,如果我创建与当前位置的假的位置,并设置标志,它出现在地图上:
for i in 0...1 {
let marker = GMSMarker()
switch i {
case 0:
marker.position = CLLocationCoordinate2DMake(self.currentLocation.coordinate.latitude + 0.01, currentLocation.coordinate.longitude)
break
case 1:
marker.position = CLLocationCoordinate2DMake(self.currentLocation.coordinate.latitude - 0.001, currentLocation.coordinate.longitude)
break
default:
break
}
marker.title = "test"
marker.map = mapView
}
为什么?
答
由于每Google Map Integration在iOS的斯威夫特3 添加地图标记。
import UIKit
import GoogleMaps
class ViewController: UIViewController {
override func loadView() {
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
}
}
+1
这是工作在项目中安装吊舱。 – Rex
答
在这里我添加了一小段代码,你可以使用它并检查你的问题。 雨燕3.0的代码
private func addMarker(obj : Model){
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: obj.latitude, longitude: obj.longitude)
marker.icon = UIImage(named: "big_map_pin");
marker.userData = obj;
marker.appearAnimation = GMSMarkerAnimation.pop
DispatchQueue.main.async {
marker.map = self.mapView;
}
}
//MARK: GOOGLE MAP DELEGATE
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
mapView.selectedMarker = marker;
return true;
}
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let calloutView = CalloutView.fromNib("CalloutView") as! CalloutView
calloutView.frame = calloutView.setWidth(width:(220))
calloutView.frame = calloutView.setHeight(height:(75))
calloutView.setNeedsLayout() //layoutIfNeeded() //
calloutView.setCalloutData(objCallout: (marker.userData as! Model))
let camreaUpdate = GMSCameraUpdate.setTarget(marker.position)
mapView.animate(with: camreaUpdate)
return calloutView;
}
答
下面是代码,您可以将您的maker.Hope将帮助
for item in data.itemsArray {
let marker = GMSMarker()
marker.position = item.coordinate
marker.groundAnchor = CGPoint(x: 0.5, y: 1)//new line add
marker.title = item.name!
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
}
Does itemsArray是否包含任何元素? – PGDev
itemsArray contains dataItem – Victor
我创建一个新项目,有同样的问题... – Victor