iOS Swift GMSMarker doesn't remove - ios

I'm using google maps and I'm trying to remove a marker.
I'm using the marker.map = nil as the guide says. But the marker remains there..
I can't use map.clear() because I don't want to lose everything...
Any tip?
/// Hide all the markers
func hideMarkers() {
let events = viewModel.events
for event in events {
event.marker?.map = nil
}
}

Official docu https://developers.google.com/maps/documentation/ios-sdk/marker?hl=en
If you wish to make modifications to a marker after you've added it to the map, ensure that you keep hold of the GMSMarker object. You can modify the marker later by making changes to this object.
let marker = GMSMarker(position: position)
marker.map = mapView
marker.map = nil
So you have hold a reference to your marker. I imagine that you're not working on your marker.
A ugly way would be to call a mapView.clear() and re-add all your markers.

Related

Reloading Google Map View when adding and removing markers

We are coding in Swift to create an application with UI buttons. These UI buttons will add or remove markers depending on its status. Because we want the buttons to be layered on top of google maps we have two view controllers. The top view controller contains a button. When the button is pressed, we want to remove the markers that have a "bad" status.
This is our code to remove the marker:
func showOnlyGood(){
mapView.clear() //this is the google map (GMSMapView.map)
for x in arrayOfGood { //Array of good markers
x.map = mapView //Set good markers to show
}
for y in arrayOfBad { //Array of bad markers
y.map = nil //removes markers from map
}
}
The google maps gets updated if the function call is in viewDidLoad(), but when we call the function in the top view controller with the buttons it does not update the map accordingly.
We think this is an issue with refreshing the google map view and have tried many different solutions, but the google map view only shows what is initially in viewDidLoad().
First, if you just want the buttons to be layered on top of the map, just add the buttons to the view after you've added the map to the view; do not create a second view controller. Your problem is most likely caused by this awkward setup. You also don't have an #objc prefix in your action method, which would definitely prevent the button from executing its action.
#objc func updateButtons() {
mapView.clear() // clear the map
for i in someArray {
let marker = GMSMarker()
// configure parameters
marker.map = mapView
}
}
That method will update your map's markers. There is [probably] never a need to refresh the map, even if you want to change styles.

List of neearby searched types IOS xcode

So this piece of code gets nearest searchedTypes(atms and banks) for google and puts markers around the map.
private func fetchNearbyPlaces(coordinate: CLLocationCoordinate2D) {
mapView.clear()
dataProvider.fetchPlacesNearCoordinate(coordinate, radius:searchRadius, types: searchedTypes) { places in
places.forEach {
let marker = PlaceMarker(place: $0)
marker.map = self.mapView
}
}
}
How do I get a table view with the list of these nearest searedTypes and get information on them to show on another viewcontroller where I can navigate from user location to that point. How the normal google maps works.
With places acquired, you can pass data to a new controller that has table view and display data there.

Google map GMSMarker warning

I have integrated google map SDK in my iOS app. It gives following warning in debug area.
Marker set as selectedMarker while not belonging to this map. Ignoring.
If any one have idea then please share it.
Whenever you create a GMSMarker, you'll want to set its map property. Check out Google's documentation here. Swift example below, but the .map call is the same in Objective-C.
let position = CLLocationCoordinate2D(latitude: 10, longitude: 10)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = mapView
Line 4 is the key here. Failing to do that will throw the error you're seeing.
The main reason is that you forget to set marker.map = mapView,
the second and the most common reason for whom interacting with markers is that you return 'false' in
mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
, the documentation said:
return YES if this delegate handled the tap event, which prevents the
map from performing its default selection behavior, and NO if the
map should continue with its default selection behavior.

Create Map Pins from XML/Array

I'm currently working on a project which grabs a bunch of traffic-cameras from a XML file and and shows them in the app. I have got my Tableview up and running, working perfectly (click on a cell, opens detailView and shows traffic-image). However, I want to display the cameralocations on a map, so users can press the pin they want to see the camera of (and it sends them to a detailView to show the camera).
I'm not sure how to make this work, any ideas?
I got the longitude and latitude coordinates. Link to the XML file: http://webkamera.vegvesen.no/metadata
It is in norwegian but, lengdegrad = longitude and breddegrad = latitude.
This is what I want to achieve (Photoshop screenshot): https://gyazo.com/93d885606efd6e6369018243b64d47e8
I don't know if this is the right place to ask, but please help if you know something :-)
Thanks in advance
To use the XML data:
Add the XML file to your project and use NSXMLParser to read it.
To create annotations on the map:
Create an annotation class based on MKAnnotationView, something like that:
class Annotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
// optionally add subtitle
init(coordinate: CLLocationCoordinate2D, title: String) {
self.coordinate = coordinate
self.title = title
}
}
To create and add the anotations to the map, for each of the parsed items from your XML file create one annotation and add it to the map. Something like this, depending on format of your parsed data. Let's say you have the data in an array called cameras:
for camera in cameras {
let coord = CLLocationCoordinate2DMake(camera.latitude, camera.longitude)
let annotation = Annotation(coordinate: coord, title: camera.title)
mapView.addAnnotation(annotation)
}

How can I remove all annotations on my MkMapView that are not currently visible on iOS 8

have a few annotations visible on my MkMapView using Swift 1.2 on iOS 8. Now if the user scrolls around the map, I'd like to remove all annotations, that are currently not visible.
How can I dow this?
First get the currently visible mapRect:
let visRect = mapView.visibleMapRect
now you can get all annotations within that rect:
let inRectAnnotations = mapView.annotationsInMapRect(visRect)
last step would be to iterate over all annotations and check if you annotation is in these annotations
for anno : MKAnnotation in mapView.annotations {
if (inRectAnnotations.contains(anno)) {
//do what you want to do with the annotation (hide/remove)
}
}

Resources