Why marker not shown in GMSMapView? - ios

Here is my code:
let camera = GMSCameraPosition.camera(withLatitude: 51.5, longitude: -0.127, zoom: 12.0)
var mainView:UIView = GMSMapView.init(frame: CGRect(x:50,y:150,width:250,height:300))
self.view.addSubview(mainView)
let mapView = GMSMapView.map(withFrame: mainView.frame, camera: camera)
mainView = mapView
//Marker Position
let position = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.127)
let london = GMSMarker(position: position)
london.title = "London"
london.icon = UIImage(named: "house")
london.map = mapView

Replace
var mainView:UIView = GMSMapView.init(frame: CGRect(x:50,y:150,width:250,height:300))
self.view.addSubview(mainView)
let mapView = GMSMapView.map(withFrame: mainView.frame, camera: camera)
mainView = mapView
with
var mainView:GMSMapView! // instance var
// in viewDidLoad
let camera = GMSCameraPosition.camera(withLatitude: 51.5, longitude: -0.127, zoom: 12.0)
mainView = GMSMapView.map(withFrame:CGRect(x:50,y:150,width:250,height:300), camera: camera)
mapView.delegate = self
self.view.addSubview(mainView)

Related

Adding title to the circle of the google map in swift

I am new in iOS development.
I am drawing a circle on user location and I want to add the title to the circle.
I am using following code
var cirlce: GMSCircle!
let myPlacelatitude = (locationManager.location?.coordinate.latitude)! let myPlacelongitude = (locationManager.location?.coordinate.longitude)!
let userLocation = GMSCameraPosition.camera(withLatitude: myPlacelatitude,
longitude: myPlacelongitude,
zoom: 7.5)
mapView.camera = userLocation
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(myPlacelatitude, myPlacelongitude)
marker.title = ""
marker.snippet = ""
marker.map = mapView
let camera = GMSCameraPosition.camera(withLatitude: myPlacelatitude,
longitude: myPlacelongitude, zoom: 6)
cirlce = GMSCircle(position: camera.target, radius: 50000)
cirlce.fillColor = UIColor.green.withAlphaComponent(0.5)
cirlce1.isTappable = true
// the title is not setting to the circle in google map
cirlce.title = "40 KM"
cirlce.map = mapView
The documentation regarding GMSCircle states that some overlays, such as markers, will display the title on the map. I am not sure if this also holds true when using GMSCircle.
However as a workaround for your issue, you can put label in the GMSCircle when using GMSMarker with UIlabel from the SO answer in the comment of Mamaessen above. Here is the code snippet incorporated in GMSCircle:
class ViewController: UIViewController {
override func loadView() {
var circle: GMSCircle!
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 9.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
circle = GMSCircle(position:camera.target , radius: 50000)
circle.fillColor = UIColor.green.withAlphaComponent(0.5)
circle.strokeColor = .blue
circle.strokeWidth = 2
circle.title = "THIS IS CIRCLE"
circle.map = mapView
let labelMarker = GMSMarker(position: camera.target)
let label = UILabel()
label.text = circle.title
label.textColor = UIColor.red
label.sizeToFit()
labelMarker.iconView = label
labelMarker.map = mapView
}
}
If you want it to show after you tap the circle just like a popup when the circle is clicked, you can use an image which is a circle and use this as an icon to your marker and your text label should be in the marker title. The following is how I implement it in the code. Just a note you can notice the line marker.icon = UIImage(named: "circle"), this is where you put the name of the image that you want as your marker. In my case I made a New Image Set in my Assets and put the image I want to use as my custom marker.
lass ViewController: UIViewController {
override func loadView() {
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 9.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.icon = UIImage(named: "circle")
marker.map = mapView
}
}
Hope this helps!

Google Maps Mapview not centering on marker

I am using google maps in ios with swift 4, i have to show a mapview in the screen, with a marker, the problem is that whenever i show the mapview i got this result:
and the result i want to get is this:
here is the code i am using:
func loadMap(location:CLLocation){
self.mapView.isMyLocationEnabled = true
self.mapView.isUserInteractionEnabled = true
self.view.layoutIfNeeded()
self.addLocationMarker()
}
func addLocationMarker(){
// Creates a marker in the center of the map.
let marker = GMSMarker()
print(self.startLocation)
marker.position = CLLocationCoordinate2D(latitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude)
marker.map = self.mapView
let camera = GMSCameraPosition.camera(withLatitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude, zoom: 12.0)
self.mapView.camera = camera
}
and the mapview is an outlet of a view in the storyboard
This Code works fine for me. Try this out (Swift 4)
func setupMapView() {
DispatchQueue.main.async {
let position = CLLocationCoordinate2D(latitude: 32.9483364, longitude: -96.8241543)
let marker = GMSMarker(position: position)
self.mapView.camera = GMSCameraPosition(target: position, zoom: 15, bearing: 0, viewingAngle: 0)
marker.title = "Enter marker title here"
marker.map = self.mapView
}
}
Note: I have used sample longitude and lattitude. You should insert your's.

Animate Map to Location is Not Working Google Maps iOS

import UIKit
import GoogleMaps
import FirebaseDatabase
import GeoFire
class MapViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
var mapView = GMSMapView()
var locationManager: CLLocationManager!
let regionRadius: CLLocationDistance = 1000
var place = CLLocationCoordinate2D()
#IBOutlet var myLocationButton: UIButton!
#IBOutlet var infoWindow: UIView!
#IBOutlet var postTitle: UILabel!
#IBOutlet var postImage: UIImageView!
var showing = false;
var pins = [String: Pin]()
var currentMarker = GMSMarker()
override func viewDidLoad() {
super.viewDidLoad()
// sets up the map view (camera, location tracker etc.)
let camera = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 17.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isMyLocationEnabled = true
mapView.delegate = self
view = mapView
self.view.addSubview(myLocationButton)
self.view.bringSubview(toFront: myLocationButton)
// Location manager
locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.startUpdatingLocation()
// Get nearby records
let geoFire = GeoFire(firebaseRef: FIRDatabase.database().reference().child("geofire"))
let query = geoFire?.query(at: CLLocation(latitude: place.latitude, longitude: place.longitude), withRadius: 0.6)
_ = query?.observe(.keyEntered, with: { (key, location) in
let marker = GMSMarker()
let newPin = Pin(title: "post", locationName: "\(key!)", discipline: "", coordinate: (location?.coordinate)!)
self.pins[newPin.locationName] = newPin
marker.icon = UIImage(named: "icon_small_shadow")
marker.position = Pin.coordinate
marker.title = Pin.title
marker.snippet = Pin.locationName
marker.map = mapView
})
myLocationTapped(myLocationButton)
}
// sets the info in the custom info window
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
if(currentMarker == marker && showing) {
infoWindow.isHidden = true
showing = false
} else {
infoWindow.isHidden = false
self.view.addSubview(infoWindow)
self.view.bringSubview(toFront: infoWindow)
postTitle.text = marker.snippet
showing = true
}
currentMarker = marker
return true
}
#IBAction func myLocationTapped(_ sender: Any) {
print("tapped")
let cameraPosition = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 15.0)
mapView.animate(to: cameraPosition)
}
I have the following code set up, designed to place a button on the google maps map that when tapped, animates the google maps camera to that location. However, my code doesn't work. The "tapped" prints in the console but the camera doesn't budge. I haven't been able to find an answer anywhere for this, so any help would be appreciated.
EDIT: Added full code for the Map View Controller
In my case the map wasn't updating because I was not calling the method on the main queue. The following code solved the issue:
DispatchQueue.main.async {
self.mapView.animate(to: camera)
}
Anything action related to the user interface should be called in the main queue
Try this way
let cameraPosition = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 15.0)
mapView.animate(to: cameraPosition)
Edit: Issue is you aren't having the reference of your map with your mapView object, change your viewDidLoad's line:
view = mapView
TO:
// sets up the map view (camera, location tracker etc.)
let camera = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 17.0)
let mapView = GMSMapView.map(withFrame: view.bounds, camera: camera)
mapView.isMyLocationEnabled = true
mapView.delegate = self
self.mapView = mapView
view.addSubview(self.mapView)

Google Maps API full screen in SWIFT3

This is the current code, after making the edits suggested, it no longer shows a marker on the map for Sydney Australia
class LocateVC: UIViewController {
#IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
//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: self.mapView.bounds, camera: camera)
self.mapView = 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
}
}
Output
This is the screen that loads
And no marker on Sydney Australia
Your are setting mapview to main view of ViewController
So Change your these two lines
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
With:
let mapView = GMSMapView.map(withFrame: self.mapView.bounds, camera: camera)
self.mapView = mapView
The answer from #Nirav is almost there
here what I did:
import UIKit
import GoogleMaps
class AddressMapController: UIViewController {
#IBOutlet weak var map123: GMSMapView! // this is linked to a UIView in storyboard
var currentLocation: CLLocation! //this is passed from the main controller
override func viewDidLoad() {
super.viewDidLoad()
let lat = currentLocation.coordinate.latitude
let long = currentLocation.coordinate.longitude
let camera = GMSCameraPosition.camera(withLatitude: lat , longitude: long, zoom: 12)
// Creates a marker in the center of the map.
let currentLocation_Marker = GMSMarker()
currentLocation_Marker.position = CLLocationCoordinate2D(latitude: lat, longitude: long)
currentLocation_Marker.title = "My Current Location"
currentLocation_Marker.snippet = "I am here now"
currentLocation_Marker.map = map123
self.map123.camera = camera
}
}
hope it will help somebody in the future

Marker not locating in google map in subview IOS SWIFT

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
-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
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 :]
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)
I have my GMSMapView inside another UIView and everything just works fine. The only different line is:
marker.map = mapView
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)
})
}
}

Resources