2017-06-01 133 views
0

在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 
} 

爲什麼?

+0

Does itemsArray是否包含任何元素? – PGDev

+0

itemsArray contains dataItem – Victor

+0

我創建一個新項目,有同樣的問題... – Victor

回答

1

由於每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

0

在這裏我添加了一小段代碼,你可以使用它並檢查你的問題。 雨燕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; 

} 
0

下面是代碼,您可以將您的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 
    }