Reputation: 65
How to create a function for drawing markers on a map from an array of applications received during the operation. That is, the function must be outside the viewDidLoad() ? If you use a simple function with the following content:
import UIKit
import GoogleMaps
import CoreLocation
class ViewController: UIViewController {
@IBOutlet var mMap: GMSMapView!
let locationManager = CLLocationManager()
let mapInsets = UIEdgeInsets(top: 10.0, left: 0.0, bottom: 100.0, right: 0.0)
override func viewDidLoad() {
super.viewDidLoad()
view = mMap
mMap.padding = mapInsets
mMap.isMyLocationEnabled = true
mMap.settings.compassButton = true
mMap.settings.myLocationButton = true
mMap.settings.scrollGestures = true
mMap.settings.zoomGestures = true
let camera = GMSCameraPosition.camera(withLatitude: myLat, longitude: myLon, zoom: 15.0)
let mMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
let buttonDps = UIButton(frame: CGRect(x: 2, y: 520, width: 103, height: 45))
button.backgroundColor = .red
button.setTitle("yes", for: .normal)
button.titleLabel!.font = UIFont.boldSystemFont(ofSize: 19)
button.layer.cornerRadius = 5.0
button.addTarget(self, action: #selector(buttonAct), for:.touchUpInside)
self.view.addSubview(button)
}
func buttonAct(sender: UIButton!) {
let alert = UIAlertController(title:"help", message:"qwerty", preferredStyle:.alert)
alert.addAction(UIAlertAction(title:"OK", style:.default){ action in
self.markercreate()
})
alert.addAction(UIAlertAction(title:"cancel", style:.cancel, handler:nil))
self.present(alert, animated:true, completion:nil)
}
func markercreate(){
let marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude: 54.9044200, longitude: 52.3154000)
marker2.title = "Россия"
marker2.snippet = "Москва"
marker2.map = mMap
}
}
then nothing happens (((
Upvotes: 0
Views: 119
Reputation: 444
Suppose you have a list of latitude, longitude and place name. Create a loop and inside your loop if you want to show markers then use this.
func createMarker()
{
let lon = Double(longResult as! String)
let lat = Double(latResult as! String)
print("Center_Name: \(centerName)")
print("Longitude: \(longResult)")
print("Latitude: \(latResult)")
let markerResult = GMSMarker()
markerResult.position = CLLocationCoordinate2D(latitude: lat! , longitude: lon!)
markerResult.title = "\(centerName)"
markerResult.map = viewMap
}
The code I have shown is a basic one. With this you can create a marker on your map.
Upvotes: 1
Reputation: 24341
As far as I can analyze,
The code will work fine. Also the marker
is added to the map
. You just don't see the marker you have added. Move the camera position
of the map
to marker
position so that you can see the marker, i.e.
func markercreate()
{
//Your code...
mMap.camera = GMSCameraPosition.camera(withLatitude: 54.9044200, longitude: 52.3154000, zoom: 15.0) //Add this line to your code
}
I assume you will resolve the 2 mMap
variables that you have created as I mentioned in the comment.
Edit:
1. In Storyboard
, set the ViewController's
view's
class
to GMSMapView
and connect the outlet
mMap
to it, i.e.
2. Follow the comments
in the below code snippet:
override func viewDidLoad()
{
super.viewDidLoad()
//Your code...
view = mMap //Remove this line
mMap.camera = GMSCameraPosition.camera(withLatitude: 54.9044200, longitude: 52.3154000, zoom: 15.0) //Add this line
let camera = GMSCameraPosition.camera(withLatitude: myLat, longitude: myLon, zoom: 15.0) //Remove this line
let mMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera) //Remove this line
//Your code...
}
Upvotes: 0