Button is Showing GMSMarkers but isn't Hiding it, How do I get it to do bothHow to round the corners of a buttonHow to hide UINavigationBar 1px bottom lineHow to create a button programmatically?How do I get a reference to the app delegate in Swift?How can I make a button have a rounded border in Swift?How to rotate GMSMarker to show user moving direction?How to hide and show UIViewController button in AppDelegate? - SwiftMultiple GMSMarker show/hide consumes timeHow do I get an image to show up on my custom button?
When using the Proficiency Dice optional rule, how should they be used in determining a character's Spell Save DC?
Is there a general term for the items in a directory?
What's "halachic" about "Esav hates Ya'akov"?
What is a Written Word™?
Has J.J.Jameson ever found out that Peter Parker is Spider-Man?
Is there a difference between `board[x, y]` and `board[x][y]` in Python?
Formal mathematical definition of renormalization group flow
Which one is more important between endgame studies and tactics?
How to win against ants
Vectorised way to calculate mean of left and right neighbours in a vector
How do I know when and if a character requires a backstory?
…down the primrose path
How does Rust's 128-bit integer `i128` work on a 64-bit system?
How long should I wait to plug in my refrigerator after unplugging it?
Is there any difference between "result in" and "end up with"?
Based on what criteria do you add/not add icons to labels within a toolbar?
Magento 2 Is it possible to use same event in multiple Modules?
What license to choose for my PhD thesis?
How do people drown while wearing a life jacket?
The warlock of firetop mountain, what's the deal with reference 192?
In MTG, was there ever a five-color deck that worked well?
Why is the Vasa Museum in Stockholm so Popular?
Is it okay to use different fingers every time while playing a song on keyboard? Is it considered a bad practice?
A Checkmate of Dubious Legality
Button is Showing GMSMarkers but isn't Hiding it, How do I get it to do both
How to round the corners of a buttonHow to hide UINavigationBar 1px bottom lineHow to create a button programmatically?How do I get a reference to the app delegate in Swift?How can I make a button have a rounded border in Swift?How to rotate GMSMarker to show user moving direction?How to hide and show UIViewController button in AppDelegate? - SwiftMultiple GMSMarker show/hide consumes timeHow do I get an image to show up on my custom button?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.
This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.
func showCarIcon()
ref = Database.database().reference()
ref.child("location").observe(.childAdded) (snapshot:DataSnapshot) in
if let dict = snapshot.value as? [String:AnyObject]
if dict["Activity"] as! String == "Car"
let longitude = dict["Longitude"] as! String
let lattitude = dict["Lattitude"] as! String
let title = dict["Title"] as! String
self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
let n = self.carIconArray.count
let heightWidth = self.mapView.frame.height
for marker in 1...n
let carMarker = GMSMarker()
let carIconView = UIImage(named: "carPin")
let image = carIconView
let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
carMarker.position = location
carMarker.icon = image
carMarker.title = title
carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
else
carMarker.map = nil
displayIt()
So this is the action function for when button is pressed.
var carNumber = String()
@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
showCarIcon()
swift xcode uibutton gmsmapview
add a comment |
Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.
This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.
func showCarIcon()
ref = Database.database().reference()
ref.child("location").observe(.childAdded) (snapshot:DataSnapshot) in
if let dict = snapshot.value as? [String:AnyObject]
if dict["Activity"] as! String == "Car"
let longitude = dict["Longitude"] as! String
let lattitude = dict["Lattitude"] as! String
let title = dict["Title"] as! String
self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
let n = self.carIconArray.count
let heightWidth = self.mapView.frame.height
for marker in 1...n
let carMarker = GMSMarker()
let carIconView = UIImage(named: "carPin")
let image = carIconView
let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
carMarker.position = location
carMarker.icon = image
carMarker.title = title
carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
else
carMarker.map = nil
displayIt()
So this is the action function for when button is pressed.
var carNumber = String()
@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
showCarIcon()
swift xcode uibutton gmsmapview
add a comment |
Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.
This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.
func showCarIcon()
ref = Database.database().reference()
ref.child("location").observe(.childAdded) (snapshot:DataSnapshot) in
if let dict = snapshot.value as? [String:AnyObject]
if dict["Activity"] as! String == "Car"
let longitude = dict["Longitude"] as! String
let lattitude = dict["Lattitude"] as! String
let title = dict["Title"] as! String
self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
let n = self.carIconArray.count
let heightWidth = self.mapView.frame.height
for marker in 1...n
let carMarker = GMSMarker()
let carIconView = UIImage(named: "carPin")
let image = carIconView
let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
carMarker.position = location
carMarker.icon = image
carMarker.title = title
carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
else
carMarker.map = nil
displayIt()
So this is the action function for when button is pressed.
var carNumber = String()
@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
showCarIcon()
swift xcode uibutton gmsmapview
Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.
This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.
func showCarIcon()
ref = Database.database().reference()
ref.child("location").observe(.childAdded) (snapshot:DataSnapshot) in
if let dict = snapshot.value as? [String:AnyObject]
if dict["Activity"] as! String == "Car"
let longitude = dict["Longitude"] as! String
let lattitude = dict["Lattitude"] as! String
let title = dict["Title"] as! String
self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
let n = self.carIconArray.count
let heightWidth = self.mapView.frame.height
for marker in 1...n
let carMarker = GMSMarker()
let carIconView = UIImage(named: "carPin")
let image = carIconView
let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
carMarker.position = location
carMarker.icon = image
carMarker.title = title
carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
else
carMarker.map = nil
displayIt()
So this is the action function for when button is pressed.
var carNumber = String()
@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
showCarIcon()
swift xcode uibutton gmsmapview
swift xcode uibutton gmsmapview
asked Mar 27 at 2:34
Tristan MellettTristan Mellett
135 bronze badges
135 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Let me explain what is issue with your code.
You are creating new marker every time when button press. So, new marker have different object id than older.
When you try to remove it, it will not works just because of it's different marker than you placed on map.
So you need to store marker in array and on remove time, get icon from array and remove it from map.
First you need to create array of GMSMarker, because you have to store every marker which is placed on map.
So, write following line of code at top of your class.
var arrCarMarkers = [GMSMarker]()
Then after, store every marker in this array which are you placing on map.
So, update your code as follow:
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
arrCarMarkers.append(carMarker) // Here is store marker in array
else
carMarker.map = nil
Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:
@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
showCarIcon()
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
self.arrCarMarkers.forEach $0.map = nil
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
Above code will remove all markers from map.
I hope this will works for you.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368935%2fbutton-is-showing-gmsmarkers-but-isnt-hiding-it-how-do-i-get-it-to-do-both%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let me explain what is issue with your code.
You are creating new marker every time when button press. So, new marker have different object id than older.
When you try to remove it, it will not works just because of it's different marker than you placed on map.
So you need to store marker in array and on remove time, get icon from array and remove it from map.
First you need to create array of GMSMarker, because you have to store every marker which is placed on map.
So, write following line of code at top of your class.
var arrCarMarkers = [GMSMarker]()
Then after, store every marker in this array which are you placing on map.
So, update your code as follow:
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
arrCarMarkers.append(carMarker) // Here is store marker in array
else
carMarker.map = nil
Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:
@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
showCarIcon()
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
self.arrCarMarkers.forEach $0.map = nil
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
Above code will remove all markers from map.
I hope this will works for you.
add a comment |
Let me explain what is issue with your code.
You are creating new marker every time when button press. So, new marker have different object id than older.
When you try to remove it, it will not works just because of it's different marker than you placed on map.
So you need to store marker in array and on remove time, get icon from array and remove it from map.
First you need to create array of GMSMarker, because you have to store every marker which is placed on map.
So, write following line of code at top of your class.
var arrCarMarkers = [GMSMarker]()
Then after, store every marker in this array which are you placing on map.
So, update your code as follow:
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
arrCarMarkers.append(carMarker) // Here is store marker in array
else
carMarker.map = nil
Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:
@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
showCarIcon()
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
self.arrCarMarkers.forEach $0.map = nil
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
Above code will remove all markers from map.
I hope this will works for you.
add a comment |
Let me explain what is issue with your code.
You are creating new marker every time when button press. So, new marker have different object id than older.
When you try to remove it, it will not works just because of it's different marker than you placed on map.
So you need to store marker in array and on remove time, get icon from array and remove it from map.
First you need to create array of GMSMarker, because you have to store every marker which is placed on map.
So, write following line of code at top of your class.
var arrCarMarkers = [GMSMarker]()
Then after, store every marker in this array which are you placing on map.
So, update your code as follow:
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
arrCarMarkers.append(carMarker) // Here is store marker in array
else
carMarker.map = nil
Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:
@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
showCarIcon()
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
self.arrCarMarkers.forEach $0.map = nil
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
Above code will remove all markers from map.
I hope this will works for you.
Let me explain what is issue with your code.
You are creating new marker every time when button press. So, new marker have different object id than older.
When you try to remove it, it will not works just because of it's different marker than you placed on map.
So you need to store marker in array and on remove time, get icon from array and remove it from map.
First you need to create array of GMSMarker, because you have to store every marker which is placed on map.
So, write following line of code at top of your class.
var arrCarMarkers = [GMSMarker]()
Then after, store every marker in this array which are you placing on map.
So, update your code as follow:
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
arrCarMarkers.append(carMarker) // Here is store marker in array
else
carMarker.map = nil
Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:
@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
showCarIcon()
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
self.arrCarMarkers.forEach $0.map = nil
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
Above code will remove all markers from map.
I hope this will works for you.
answered Mar 27 at 5:18
Sagar ChauhanSagar Chauhan
2,8502 gold badges10 silver badges39 bronze badges
2,8502 gold badges10 silver badges39 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368935%2fbutton-is-showing-gmsmarkers-but-isnt-hiding-it-how-do-i-get-it-to-do-both%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown