3

Hello i was trying to put map in subview but when i put google map in sub view it doesn't work marker and GPS Coordinates don't work

-With Sub View With Sub View

-Without Sub View Without Sub View

-SWIFT CODE

import UIKit
import GoogleMaps

class HomeViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: GMSMapView!

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()

                let camera = GMSCameraPosition.cameraWithLatitude(15.4989, longitude: 73.8278, zoom: 6)
                let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
                mapView.myLocationEnabled = true
//                self.view = mapView
                self.view.addSubview(mapView)
                let marker = GMSMarker()
                marker.position = CLLocationCoordinate2DMake(15.4989, 73.8278)
                marker.title = "Panjim"
                marker.snippet = "Near Don Bosco,Alphran Plaza"
                marker.map?.addSubview(mapView)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

Thanks in advance

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
O-mkar
  • 5,430
  • 8
  • 37
  • 61
  • You use `let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)` which will make the `mapView` has zero size. Also, like @mixth suggested, you should use `marker.map = mapView` instead, to add marker to your `mapView`. – ztan Oct 22 '15 at 21:14
  • if i do like that it comes out of the view – O-mkar Oct 23 '15 at 05:29
  • check here https://stackoverflow.com/questions/15417811/cannot-put-a-google-maps-gmsmapview-in-a-subview-of-main-main-view/48517389#48517389 – Heshan Sandeepa Jan 30 '18 at 08:57

4 Answers4

6

I found the solution. The problem was : i was creating a new map, then was adding a marker to that new map. Then with the new map i was doing nothing. So here is my Solution :

@IBOutlet weak var subviewMap: GMSMapView!

func loadMap() {
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
        subviewMap.camera = camera
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = subviewMap
    }

And it works.

NOTE : Do not forget to Mention your subview as GMSMapView class in IB

Thanks @O-mkar and @mixth for efforts.

Happy Coding :]

SML
  • 2,172
  • 4
  • 30
  • 46
3

Here is the solution to add marker

            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2DMake(lat, long)
            marker.appearAnimation = kGMSMarkerAnimationPop
            marker.title = "Marker" // Marker title here
            marker.snippet = "Tap the ↱ Navigate button to start navigating."
            marker.infoWindowAnchor = CGPoint(x: 0.5, y: 0)
            marker.icon = UIImage(named: "marker") //Set marker icon here
            marker.map = self.mapView // Mapview here

Animate Camera to position

    let camera = GMSCameraPosition.camera(withLatitude: 15.4989, longitude: 73.8278, zoom: 17)
    mapView.animate(to: camera)
O-mkar
  • 5,430
  • 8
  • 37
  • 61
1

I have my GMSMapView inside another UIView and everything just works fine. The only different line is:

marker.map = mapView
O-mkar
  • 5,430
  • 8
  • 37
  • 61
mixth
  • 636
  • 5
  • 16
1
after adding marker you should add some delay with this approach i have added 2 marker with bounds 

DispatchQueue.main.async {
            if self.markerArray.count > 1 {
                var bounds = GMSCoordinateBounds()
                for marker in self.markerArray {
                    marker.map = self.mapView
                    bounds = bounds.includingCoordinate(marker.position)
                }
                self.isMovedTheMap = false
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.9, execute: {
                    self.superTopView.fadeOut()
                    let update = GMSCameraUpdate.fit(bounds, withPadding: 80)
                    self.mapView.animate(with: update)
                })
            }
        }