2017-06-01 6 views
0

In Swift lade ich Standortdaten herunter und setze einen Marker auf Google Maps, aber der Marker erscheint nicht auf der Karte.Google Maps GMSMarker wird nicht auf der Karte angezeigt

Daten Artikel:

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

Set Marker (Markierung auf der Karte nicht angezeigt):

let data = UserData.shareInstance() 
for item in data.itemsArray { 
    let marker = GMSMarker() 
    marker.position = item.coordinate 
    marker.title = item.name! 
    marker.map = mapView 
} 

Aber wenn ich eine gefälschte Position mit der aktuellen Position erstellen und die Markierung gesetzt, Es erscheint auf der Karte:

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 
} 

Warum?

+0

Enthält itemsArray irgendwelche Elemente? – PGDev

+0

itemsArray enthält dataItem – Victor

+0

I Erstellen Sie ein neues Projekt, hat das gleiche Problem ... – Victor

Antwort

1

Gemäß der Google Map Integration in iOS Swift 3. Hinzufügen einer Karte mit Marker helfen wird.

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

Dies funktioniert mit Pod-Installation im Projekt. – Rex

0

Hier habe ich Code-Schnipsel hinzugefügt, Sie können es verwenden und Ihr Problem überprüfen. Swift 3.0-Code

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

Hier ist der Code, den Sie Ihre maker.Hope Set kann es

 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 
    } 
Verwandte Themen