Google地图GMSMarker未显示在地图上

时间:2017-06-01 05:13:44

标签: swift google-maps

在Swift中,我下载了位置数据并在Google地图上设置了标记,但该标记未显示在地图上。

数据项:

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
}

为什么?

3 个答案:

答案 0 :(得分:1)

根据iOS Swift 3中的Google Map Integration使用标记添加地图。

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 :(得分:0)

在这里我添加了代码片段,您可以使用它并检查您的问题。 Swift 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;

}

答案 2 :(得分:0)

以下是您可以设置制造商的代码。希望它会有所帮助

     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
    }