How to set/update zoom level to GMSMapView in Swift? - swift

Following code is how I got new marker position and update mapview.
if self.state.dropOff != nil {
let loc = response
let position = CLLocationCoordinate2D(latitude: loc.latitude!, longitude: loc.longitude!)
self.getPolylineRoute(from: self.state.pickUp!.coordinate, to: self.state.dropOff!.coordinate)
CATransaction.begin()
CATransaction.setAnimationDuration(1.0)
if self.acceptedCabMarker == nil {
self.acceptedCabMarker = GMSMarker(position: position)
}
self.acceptedCabMarker!.position = position
self.acceptedCabMarker!.isFlat = true
self.acceptedCabMarker!.icon = UIImage(named: markerIcon)
self.acceptedCabMarker!.setIconSize(scaledToSize: .init(width: 40, height: 40))
self.acceptedCabMarker!.appearAnimation = .pop
self.acceptedCabMarker!.rotation = CLLocationDegrees(loc.bearing ?? 0)
CATransaction.commit()
DispatchQueue.main.async {
self.acceptedCabMarker!.map = self.mapView
}
}
Problem is everytime this code is executed, mapview zoom level became to its original state. Which mean user can't zoom the map for long.
I tried to save the zoom using method.
extension SomeHomeViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
print("Camera Zoom: \(position.zoom)")
currentPosition = position
}
}
But I can't reuse currentPosition because
self.mapView?.camera.zoom = currentPosition?.zoom
is not allowed.

You can simply set the zoom on GMSMapView in this way
let camera = GMSCameraPosition.camera(withLatitude: loc.latitude, longitude: loc.longitude, zoom: 10.0)
self.mapView.camera = camera

You need to use
- (void)animateToZoom:(float)zoom;
method which is defined in GMSMapView (Animation) Category. For more info you can refer this link.

Related

Google Maps iOS SDK GMSURLTileLayer displaying incorrect color/transparency

PNG Tiles are showing incorrect color and transparency on iOS. Transparent areas show up as a semi-transparent white. The same tile set shows up correctly on Android and web browser, so I don't believe it is an issue is with the tiles themselves. Here's my Swift code:
class ViewController: UIViewController {
#IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 30.00, longitude: -90, zoom: 7.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
// Implement GMSTileURLConstructor
let urls: GMSTileURLConstructor = { (x, y, zoom) in
let url = "https://aerocast.s3.amazonaws.com/radar/klix/lixtileskml/\(zoom)/\(x)/\(y).png"
return URL(string: url)
}
let tilelayer = GMSURLTileLayer(urlConstructor: urls)
tilelayer.zIndex = 100
tilelayer.map = mapView
tilelayer.opacity = 1.0
}
}
I've looked for some way to adjust the color properties of the tilelayer, but have not been able to find anything.
The issue was specific to the iOS emulator on xcode. The issue is not present on a physical iOS device. I brought up the problem to Google Maps support and they were able to replicate the problem and are looking into it.
I think there is some problem with view hierarchy
class ViewController: UIViewController {
#IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 30.00, longitude: -90, zoom: 7.0)
self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view.addSubview(mapView)
// Define constraints (i.e position in the parent view), example for full screen ones
mapView.translatesautoresizingmaskintoconstraints = false
mapView.topAnchor.constraint(equalToSystemSpacingBelow: view.topAnchor).isActivate = true
mapView.bottomAnchor.constraint(equalToSystemSpacingBelow: view.bottomAnchor).isActivate = true
mapView.leftAnchor.constraint(equalToSystemSpacingBelow: view.leftAnchor).isActivate = true
mapView.rightAnchor.constraint(equalToSystemSpacingBelow: view.rightAnchor).isActivate = true
// Implement GMSTileURLConstructor
let urls: GMSTileURLConstructor = { (x, y, zoom) in
let url = "https://aerocast.s3.amazonaws.com/radar/klix/lixtileskml/\(zoom)/\(x)/\(y).png"
return URL(string: url)
}
let tilelayer = GMSURLTileLayer(urlConstructor: urls)
tilelayer.zIndex = 100
tilelayer.map = mapView
tilelayer.opacity = 1.0
}
}

Altering Existing MKOverlays - How to identify which to alter?

I am trying to change the color of an existing MKOverlay on a map.
I am add several MKPolygons as unique overlays to mapView. As they are rendered, the overlay's color is applied by the mapView rendererFor function.
Periodically I would like to change the color of an existing overlay; ideally without removingAll and re-adding. I have code that will handle the color change, but I do not know how to identify different overlays other than by their type (MKPolygon, MKCircle) - but all overlays in my situation are MKPolygon, so executing the code simply changes all.
I have tried converting the MKPolygon into an MKOverlay before using addOverlay and manipulating the title, but title is "get-only"
I have tried subclassing MKPolygon, but my reading says this is bad in Swift.
I add the overlay like this.
var shapeNW:MKMapPoint = MKMapPoint( CLLocationCoordinate2D(latitude: 48.313319, longitude: -124.109715))
var shapeNE:MKMapPoint = MKMapPoint( CLLocationCoordinate2D(latitude: 48.313312, longitude: -124.108695))
var shapeSW:MKMapPoint = MKMapPoint( CLLocationCoordinate2D(latitude: 48.312661, longitude: -124.109767))
var shapeSE:MKMapPoint = MKMapPoint( CLLocationCoordinate2D(latitude: 48.312626, longitude: -124.108679))
var myShapePoints:[MKMapPoint] = [shapeNW,shapeNE,shapeSE,shapeSW]
// addAnItemToMap(title: "Circle", locationName: "CircleName", type: "SS", coordinate: feiLocation, horizontalAccuracy: 20)
var myShape:MKPolygon = MKPolygon(points: myShapePoints, count: 4)
mapView.addOverlay(myShape)
Here is my function that I use to change the color after it is created. But obviously this function changes the color of all items as it cannot identify which overlay to change. This is my issue, I would like to identify the specific overlay and change just it.
func changeColor(identifier: String) {
let overlays = self.mapView.overlays
let duration:TimeInterval = 5.0
for overlay in overlays {
if identifier == "on"{
let renderer = mapView.renderer(for: overlay) as! MKPolygonRenderer
DispatchQueue.main.async{
renderer.alpha = 1.0
renderer.fillColor = UIColor.blue
}
}
else {
let renderer = mapView.renderer(for: overlay) as! MKPolygonRenderer
DispatchQueue.main.async{
renderer.alpha = 0.0
}
}
}
}
You can just subclass MKPolygon:
class CustomPolygon: MKPolygon {
var identifier: String? = nil
}
Then you can configure your renderer accordingly:
func configureColor(of renderer: MKPolygonRenderer, for overlay: MKOverlay) {
let baseColor: UIColor
if let polygon = overlay as? CustomPolygon, polygon.identifier == "on" {
baseColor = .cyan
} else {
baseColor = .red
}
renderer.strokeColor = baseColor.withAlphaComponent(0.75)
renderer.fillColor = baseColor.withAlphaComponent(0.25)
}
Then your main renderer(for:) can use that:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolygonRenderer(overlay: overlay)
renderer.lineWidth = 5
configureColor(of: renderer, for: overlay)
return renderer
}
}
As can your update routine can:
func updateColors() {
for overlay in mapView.overlays {
if let renderer = mapView.renderer(for: overlay) as? MKPolygonRenderer {
configureColor(of: renderer, for: overlay)
}
}
}
Personally, rather than having overlays whose identifier is not "on" have an alpha of 0, I just would remove those overlays from the map, and then you don’t have to deal with any of this.

How to delay callout from showing when annotation selected in MKMapView? Swift 4

(This is my first stack overflow question haha)
UPDATE:
From this link - Center MKMapView BEFORE displaying callout
I implemented the solution from Thermometer, however selecting and deselecting the annotation makes it look like my application is glitching.
I think the best way would be to delay the callOut (detail pop up) from appearing by a few seconds so the map has time to move first.
Here is my code:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotation = view.annotation else {
return
}
let currentAnnotation = view.annotation as? MapMarker
Global.currentAnnotation = currentAnnotation
findRelatinoshipLines()
if Global.showLifeStoryLines {
var locations = lifeStoryAnnotations.map { $0.coordinate }
let polyline = MKPolyline(coordinates: &locations, count: locations.count)
Global.finalLineColor = Global.lifeStoryColor
mapView.addOverlay(polyline)
}
if Global.showFatherLines {
var fatherLocations = fatherTreeAnnotations.map { $0.coordinate }
let fatherPolyline = MKPolyline(coordinates: &fatherLocations, count: fatherLocations.count)
Global.finalLineColor = Global.fatherLineageColor
mapView.addOverlay(fatherPolyline)
}
if Global.showMotherLines {
var motherLocations = motherTreeAnnotations.map { $0.coordinate }
let motherPolyline = MKPolyline(coordinates: &motherLocations, count: motherLocations.count)
Global.finalLineColor = Global.motherLineageColor
mapView.addOverlay(motherPolyline)
}
if Global.showSpouseLines {
var locations = spouseAnnotations.map { $0.coordinate }
let polyline = MKPolyline(coordinates: &locations, count: locations.count)
Global.finalLineColor = Global.spouseColor
mapView.addOverlay(polyline)
}
if Global.zoomChange == true {
Global.zoomChange = false
} else {
let currentRegion = mapView.region
let span = currentRegion.span
let location = currentAnnotation!.coordinate
let region = MKCoordinateRegion(center: location, span: span)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
mapView.setCenter(annotation.coordinate, animated: true)
//mapView.setRegion(region, animated: true)
}
}
}
CONTINUED:
Basically I'm working on a family genealogy application that displays events from relatives on a map.
When I click an annotation (event) the details (who event belongs to, where and when, etc) pops up above with an information button to show the selected person.
I have it set up to set the MKMapView region so that the selected annotation is centered each time a new annotation is clicked.
The problem is when I click an event that is on the edge of the screen, my annotation title/description pops up off centered so that it fits on my screen because it doesn't know that I plan on re-centering the map view around said annotation.
I was wondering if there was any way to make the title/description appear centered directly above the selected annotation so that when I move the map everything is centered and fits on the screen.
Here are some screenshots of what I'm talking about:
Before and After
Solved it by calling setCenter with a slight delay in mapView(_:didSelect:):
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotation = view.annotation else {
return
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
mapView.setCenter(annotation.coordinate, animated: true)
}
}

Not able to add Multiple Markers in GSMapView xCode Swift 3

I am trying to add multiple Markers on Googlemaps in my app.
In the viewcontrolller under viewDidLoad I am able to load the map and a single marker.
override func viewDidLoad() {
super.viewDidLoad()
title = NSLocalizedString("section_map", comment: "test")
let camera = GMSCameraPosition.camera(withLatitude: 48.7784, longitude:9.18121, zoom: 12)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
view = mapView
mapView.settings.myLocationButton = true
mapView.delegate = self
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 48.7784,longitude: 9.18121)
marker.title = "title"
marker.snippet = "snipple"
marker.icon = UIImage(named:"pin_you")
marker.map = mapView
mapData()
}
It call mapData() and from there is json file is generated
after parsing setPin is called to set the markers
func setPin(){
DispatchQueue.main.async {
for item in self.mapItems {
print (" \(item.name) \(item.marker) \(item.latitude) \(item.longitude)")
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: item.latitude,longitude: item.longitude)
marker.title = item.name
marker.snippet = item.fulladdress
var iconImage: String
switch (item.marker){
case 1:
iconImage = "pin_silver"
case 2:
iconImage = "pin_blue"
case 3:
iconImage = "pin_gold"
case 6:
iconImage = "pin_you"
default:
iconImage = "pin_silver"
}
marker.icon = UIImage(named:iconImage)
marker.map = self.mapView
}
}
}
The pins are not shown.
The print in for item in self.mapItems shows
Position number A 1 48.76947562 9.15440351
Position number B 1 48.75716485 9.17081058
Position number C 1 48.81191625 9.22752149
Position number D 2 48.81192516 9.22766708
this means all the proper data is available.
However the map is there the one pin made in viewDidLoad
The Markers in function setPin are not shown or maybe not set.
Does any-one have an idea?
I have solved it in have changed the mapData(mapView: GMSMapView!) also for setPin(mapView: GMSMapView!)
func setpin(mapView: GMSMapView!){
DispatchQueue.main.async {
for item in self.mapItems {
print (" \(item.name) \(item.marker) \(item.latitude) \(item.longitude)")
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: item.latitude,longitude: item.longitude)
marker.title = item.name
marker.snippet = item.fulladdress
var iconImage: String
switch (item.marker){
case 1:
iconImage = "pin_silver"
case 2:
iconImage = "pin_blue"
case 3:
iconImage = "pin_gold"
case 6:
iconImage = "pin_you"
default:
iconImage = "pin_silver"
}
marker.icon = UIImage(named:iconImage)
marker.map = mapView
}
}
}
Clustering your markers, you can put a large number of markers on a map without making the map hard to read. The marker clustering utility helps you manage multiple markers at different zoom levels.
For the full code sample, see the ObjCDemoApp and SwiftDemoApp on GitHub.
To add a simple marker clusterer.
/// Point of Interest Item which implements the GMUClusterItem protocol.
class POIItem: NSObject, GMUClusterItem {
var position: CLLocationCoordinate2D
var name: String!
init(position: CLLocationCoordinate2D, name: String) {
self.position = position
self.name = name
}
}
The following code creates a cluster manager using the GMUNonHierarchicalDistanceBasedAlgorithm and the MUDefaultClusterRenderer that are included in the utility library:
class ViewController: UIViewController, GMUClusterManagerDelegate,
GMSMapViewDelegate {
private var mapView: GMSMapView!
private var clusterManager: GMUClusterManager!
override func viewDidLoad() {
super.viewDidLoad()
// Set up the cluster manager with the supplied icon generator and
// renderer.
let iconGenerator = GMUDefaultClusterIconGenerator()
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: mapView,
clusterIconGenerator: iconGenerator)
clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm,
renderer: renderer)
// Generate and add random items to the cluster manager.
generateClusterItems()
// Call cluster() after items have been added to perform the clustering
// and rendering on map.
clusterManager.cluster()
}
}
Feed your markers into the cluster as GMUClusterItem objects by calling clusterManager:addItem:. The following code randomly generates cluster items (POIs) within the scope of the map's camera, then feeds them to the cluster manager:
/// Randomly generates cluster items within some extent of the camera and
/// adds them to the cluster manager.
private func generateClusterItems() {
let extent = 0.2
for index in 1...kClusterItemCount {
let lat = kCameraLatitude + extent * randomScale()
let lng = kCameraLongitude + extent * randomScale()
let name = "Item \(index)"
let item =
POIItem(position: CLLocationCoordinate2DMake(lat, lng), name: name)
clusterManager.addItem(item)
}
}
/// Returns a random value between -1.0 and 1.0.
private func randomScale() -> Double {
return Double(arc4random()) / Double(UINT32_MAX) * 2.0 - 1.0
}
Handle events on markers and clusters
class ViewController: UIViewController, GMUClusterManagerDelegate, GMSMapViewDelegate {
private var mapView: GMSMapView!
private var clusterManager: GMUClusterManager!
override func viewDidLoad() {
super.viewDidLoad()
// ... Rest of code omitted for easy reading.
// Register self to listen to both GMUClusterManagerDelegate and
// GMSMapViewDelegate events.
clusterManager.setDelegate(self, mapDelegate: self)
}
// MARK: - GMUClusterManagerDelegate
func clusterManager(clusterManager: GMUClusterManager, didTapCluster cluster: GMUCluster) {
let newCamera = GMSCameraPosition.cameraWithTarget(cluster.position,
zoom: mapView.camera.zoom + 1)
let update = GMSCameraUpdate.setCamera(newCamera)
mapView.moveCamera(update)
}
// MARK: - GMUMapViewDelegate
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
if let poiItem = marker.userData as? POIItem {
NSLog("Did tap marker for cluster item \(poiItem.name)")
} else {
NSLog("Did tap a normal marker")
}
return false
}
}
For more Details please follow the Here

How to update a GMSMapView with GMSCameraUpdate

I call animateWithCameraUpdate on a GMSMapView expecting it to change the map view to show the new GMSCoordinateBounds but it has no effect.
My map loads in a UICollectionViewReusableView to initially display Western Europe:
#IBOutlet weak var mapView: GMSMapView!
var fetchedResultsController : NSFetchedResultsController!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let camera = GMSCameraPosition.cameraWithLatitude(51.48, longitude: 0, zoom: 4)
mapView.camera = camera
}
I then call a function to query all my locations and update the GMSMapView to show all my locations:
func plotAll(){
let bounds = GMSCoordinateBounds.init()
for property in fetchedResultsController.fetchedObjects!
{
let capitalAsset : CapitalAsset = property as! CapitalAsset
let marker = GMSMarker.init()
marker.draggable = false
marker.snippet = capitalAsset.address
let location = CLLocationCoordinate2DMake(Double(capitalAsset.latitude!), Double(capitalAsset.longitude!))
marker.position = location
marker.map = mapView
// Update bounds to include marker
bounds.includingCoordinate(marker.position)
}
mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))
}
My plotAll function is successfully called and loops through a dozen global locations adding these to the GMSCoordinateBounds.
But the map is not being updated. I was expecting the map view to change when I called animateWithCameraUpdate but it has no effect.
Further information, for debugging I replaced the line
mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))
with :
let camera = GMSCameraPosition.cameraWithLatitude(51.48, longitude: 0, zoom: 10)
mapView.camera = camera
This does update my map view so there is no problem with calling my plotAll function, the issue is probably in my use of animateWithCameraUpdate.
I got this to work by replacing :
mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))
with:
let camera = mapView.cameraForBounds(bounds, insets:UIEdgeInsetsZero)
mapView.camera = camera;