Reputation: 159
I am Making demo of Google map i have add marker in google map that about 20-30 marker on google map so i want to do when user enter place name in textfield that place will be display and it's marker and all previous marker which i add in google map that should also display. so i have add marker when user enter place in UITextField..i successfully did all task..but when user search another place the previous search marker place still that position..so i don't know how to remove previous marker when user search another place..
//here is my code
//IBOutlet
@IBOutlet var ViewMap: GMSMapView!
func GetLocationFromAddress(address: String) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error ?? "")
}
if let placemark = placemarks?.first {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
print("lat", coordinates.latitude)
print("long", coordinates.longitude)
let position = CLLocationCoordinate2D(latitude: coordinates.latitude, longitude: coordinates.longitude)
let marker = GMSMarker(position: position)
marker.title = "Name Of Location"
marker.map = self.ViewMap
let camera = GMSCameraPosition.camera(withLatitude: coordinates.latitude,
longitude: coordinates.longitude,
zoom: self.zoomLevel)
self.ViewMap.camera = camera
self.ViewMap.animate(to: camera)
}
})
}
//function call
@IBAction func btnSearchAction(_ sender: Any) {
GetLocationFromAddress(address: self.txtSearch.text!)
}
Any of your help make my day good..thanks in advance!!!!
Upvotes: 1
Views: 599
Reputation: 191
I think you should save last search marker in variable like searchedMarker of type GMSMarker.
and next time when you search again for any location and you get action in btnSearchAction method.
@IBAction func btnSearchAction(_ sender: Any)
{
searchedMarker.map = nil
GetLocationFromAddress(address: self.txtSearch.text!)
}
and also update searchedMarker in GetLocationFromAddress method on creation of GMSMarker.
Upvotes: 2