0

In Swift, I download location data and set a marker on Google Maps, but the marker does not appear on the map.

Data item:

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 (marker does not appear on the map):

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

But, if I create a fake position with the current location and set the marker, it appears on the map:

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
}

Why?

Pang
  • 9,564
  • 146
  • 81
  • 122
Victor
  • 109
  • 1
  • 2
  • 11

3 Answers3

2

As per the Google Map Integration in iOS Swift 3. Adding a Map With Marker.

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

Here i added snippet of code, you can use it and check your problem. 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;

}
Jay Mehta
  • 1,431
  • 19
  • 40
0

Here is code that you can Set your maker.Hope it will be help

     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
    }
Morshed Alam
  • 153
  • 2
  • 10