Google Maps GMSMaker iconView - iOS SDK - ios

Error:
value of type GMSmarker has no member iconView
How can I use iconView?
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 13.7990609, longitude: 100.5494513)
marker.title = "Coffee Factory"
marker.snippet = "Chatuchak"
marker.iconView = imageView
marker.map = mapView

Make sure that you're using the latest Google Maps SDK v2.3.0 and above.
Try this:
marker.icon = UIImage(named: "<Your Image>")
EDIT:
Or you can set UIImageView to iconView
marker.iconView = UIImageView(image: UIImage(named: "<Your Image>"))
Output:
Hope it helps.

you can try
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: restaurant.latitude, longitude: restaurant.longitude)
marker.title = restaurant.name
marker.icon = xyz

Here it's what i'm done with this. Hope it will also work for you.
Swift 3.0
let marker = GMSMarker()
let markerImage = UIImage(named: "mapMarker")!.withRenderingMode(.alwaysTemplate)
let markerView = UIImageView(image: markerImage)
marker.position = CLLocationCoordinate2D(latitude: 33.5067, longitude: 51.2045)
marker.iconView = markerView
marker.map = mapView

Related

How to animate Google Map to current marker positions and draw lines in Swift

I am getting locations from Firestore and successfully show the marker on particular locations on Google Map but the problem I am facing when I run the app in Simulator it is not animating to the current marker positions. And I don't know how can I draw the route between the markers. Please help?
Here is the code of getting locations from Firestore in swift.
for document in snapshot!.documents {
print(document.data())
let marker = GMSMarker()
self.location.append(Location(trackingData: document.data()))
print(self.location)
let latitude = document.data()["Latitude"] ?? 0
print(latitude)
let longitude = document.data()["longitude"] ?? 0
print(longitude)
marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees , longitude: longitude as! CLLocationDegrees)
marker.map = self.mapView
marker.userData = self.location
marker.icon = UIImage(named: "marker")
print("Data stored in marker \(marker.userData!)")
}
You can achieve this with GMSCoordinateBounds
Reference: https://stackoverflow.com/a/45169325/8447312
Have a bounds object before your for loop:
var bounds = GMSCoordinateBounds()
Then in your for loop you will make each marker's position to be included in bounds object:
var bounds = GMSCoordinateBounds()
for document in snapshot!.documents {
print(document.data())
let marker = GMSMarker()
self.location.append(Location(trackingData: document.data()))
let latitude = document.data()["Latitude"] ?? 0
let longitude = document.data()["longitude"] ?? 0
marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees , longitude: longitude as! CLLocationDegrees)
marker.map = self.mapView
marker.userData = self.location
marker.icon = UIImage(named: "marker")
//include position for each marker
bounds = bounds.includingCoordinate(marker.position)
}
//when your loop is completed you included all your marker's coordinate in your bounds now, you need to animate:
mapView.animate(with: GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0)))
I gave 20 insets for each side, you can change it to a value of your desire.
About drawing lines try this:
Create a path right above you create bounds:
let path = GMSMutablePath()
Then in your for loop add each marker's position to this path like you did for bounds:
path.add(marker.position)
At the end before animating your mapView create directions like:
let directions = GMSPolyline(path: path)
directions.strokeWidth = 1.0
directions.map = mapView
Whole code (paths added):
var bounds = GMSCoordinateBounds()
var path = GMSMutablePath()
for document in snapshot!.documents {
print(document.data())
let marker = GMSMarker()
self.location.append(Location(trackingData: document.data()))
let latitude = document.data()["Latitude"] ?? 0
let longitude = document.data()["longitude"] ?? 0
marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees , longitude: longitude as! CLLocationDegrees)
marker.map = self.mapView
marker.userData = self.location
marker.icon = UIImage(named: "marker")
bounds = bounds.includingCoordinate(marker.position)
path.add(marker.position)
}
let directions = GMSPolyline(path: path)
directions.strokeWidth = 1.0
directions.map = mapView
mapView.animate(with: GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0)))

Resetting/Updating Markers in Google Maps ios (swift3)

I am building an ios application that uses a google map to display the location of the places return in the server. I manage to display the result in the initial load of the map, but when I want to search new plaaces and reload the markers in the map. Unfortunately the app was crashes.
Hope for your help.
Thank you.
Here is my code in the ViewController to initially display the markers:
func setUpMap() {
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
self.mapView.camera = camera
let map_view = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
for data in arrData {
let long = data["LONGITUDE"].string!
let lat = data["LATITUDE"].string!
if long != "" || lat != "" {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
marker.title = ""
marker.snippet = "Hey, this is sample"
marker.icon = UIImage(named: "locator")
marker.map = self.mapView
bounds = bounds.includingCoordinate(marker.position)
}
}
// fit to bounds
mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
self.mapView = map_view
}
Here is my code to reload/reset the markers in the map.
func reloadMarkers() {
self.mapView.clear()
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
self.mapView.camera = camera
let map_view = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
var _bounds = GMSCoordinateBounds()
for data in arrData {
let long = data["LONGITUDE"].string!
let lat = data["LATITUDE"].string!
if long != "" || lat != "" {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
marker.title = ""
marker.snippet = "Hey, this is sample"
marker.icon = UIImage(named: "locator")
marker.map = self.mapView
_bounds = _bounds.includingCoordinate(marker.position)
}
}
// fit to bounds
mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
self.mapView = map_view
}
Thanks for the help.
Cheers!
I found a solution, please see sample code below.
func reloadMarkers() {
let camera = GMSCameraPosition.camera(withLatitude: 12.0377995, longitude: 122.6408281, zoom: 10.0)
let map_view = GMSMapView.map(withFrame: self.brachMapView.bounds, camera: camera)
map_view.camera = camera
var bounds = GMSCoordinateBounds()
for data in arrData {
let long = data["LONGITUDE"].string!
let lat = data["LATITUDE"].string!
if long != "" || lat != "" {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
marker.title = ""
marker.snippet = "Hey, this is sample"
marker.icon = UIImage(named: "locator")
marker.map = map_view
bounds = bounds.includingCoordinate(marker.position)
}
}
// fit to bounds
map_view.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
self.brachMapView.addSubview(map_view)
}
Thanks.
Cheers

How to show custom images on google maps using swift

How can I show these kind of car images in picture on google maps
according to latitude & longitude?
I tried the following code, but I got the following incorrect result
let marker1 = GMSMarker()
let marker2 = GMSMarker()
let marker3 = GMSMarker()
let marker4 = GMSMarker()
let marker5 = GMSMarker()
let markerImage = UIImage(named: "white_car_icon copy.png")!.withRenderingMode(.alwaysTemplate)
let markerView = UIImageView(image: markerImage)
marker1.position = CLLocationCoordinate2D(latitude: 12.9077923, longitude: 77.586962)
marker2.position = CLLocationCoordinate2D(latitude: 12.9077923, longitude: 77.586962)
marker3.position = CLLocationCoordinate2D(latitude: 12.9077856, longitude: 77.58695950000001)
marker4.position = CLLocationCoordinate2D(latitude: 12.90782858, longitude: 77.58678956)
marker5.position = CLLocationCoordinate2D(latitude: 12.90780888511306, longitude: 77.58487723767757)
marker1.iconView = markerView
marker1.map = mapView
marker2.iconView = markerView
marker2.map = mapView
marker3.iconView = markerView
marker3.map = mapView
marker4.iconView = markerView
marker4.map = mapView
marker5.iconView = markerView
marker5.map = mapView
If anyone could help me with this, I'd be thankful to you. I have been trying it for the past 4 days, but didn't get correct output.
The right option is using the icon property
marker5.icon = UIImage(named: "paradero")
With this you can set any image you want.

How to load google map polyline view in tableview cell?

I am using google's direction api to get all the Route information. From the response i am passing value of "overview_polyline" to the GMSPath object. Unfortunately it's not showing map . I have applied same logic in previous screen and its loading polyline perfectly. Are my cell's initialized before map loads?
let camera = GMSCameraPosition.camera(withLatitude: searchLocation.coordinate.latitude,
longitude: searchLocation.coordinate.longitude, zoom: 8)
let mapView = GMSMapView.map(withFrame: cell.viaMap.frame, camera: camera)
let center = CLLocationCoordinate2D(latitude: MomentaryLatitude, longitude: MomentaryLongitude)
mapView.isMyLocationEnabled = true
cell.viaMap.addSubview(mapView)
let marker = GMSMarker()
marker.title = "Current Location"
marker.snippet = "XXX"
marker.map = cell.viaMap
let path: GMSPath = GMSPath(fromEncodedPath: myPolyLine)!
let routePolyline = GMSPolyline(path: path)
routePolyline.map = cell.viaMap
routePolyline.strokeWidth = 50
routePolyline.strokeColor = UIColor.red
routePolyline.map = cell.viaMap
var bounds = GMSCoordinateBounds()
let pathInt = Int((path.count()))
for i in 0 ..< pathInt{
bounds = bounds.includingCoordinate(path.coordinate(at: UInt(i)))
}
cell.viaMap.animate(with: GMSCameraUpdate.fit(bounds))

How to show a Info window in iOS Google maps without tapping on Marker?

I am new to iOS development. This is regarding Marker info window in Google Maps iOS SDK.
I understand, we can create a marker with info window using GMSMarkerOption.
GMSMarkerOption *myLocationOption = [GMSMarkerOption alloc];
myLocationOption .title = #"My Location";
myLocationOption .snippet = #"Lat:...., Lang:....";
[mapView addMarkerOption:myLocationOption];
As per the above code, Marker displayed in the Map View as expected.
And tapping on marker shows the "My Location" info window in Google maps which is good.
Is there anyway we can show the info window programmatically when the user goes to Custom Map Screen?
This has changed on Google Maps SDK and it's easier to understand:
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = coordinate;
marker.title = #"Location selected";
marker.snippet = #"Testing";
marker.map = mapView_;
//Show info window on map
[mapView_ setSelectedMarker:marker];
You use now setSelectedMarker method to show an info window of a marker
GMSMarkerOptions *myLocationOptions = [GMSMarkerOptions options];
myLocationOptions.title = #"My Location";
myLocationOptions.snippet = #"Lat:...., Lang:....";
mapView.selectedMarker = [mapView addMarkerWithOptions:myLocationOptions];
(note that it's Options, not Option)
Swift 3.0
func addMarker(_ location:CLLocation){
var locationMarker: GMSMarker!
if locationMarker != nil {
locationMarker.map = nil
}
locationMarker = GMSMarker(position: location.coordinate)
locationMarker.map = mapView
locationMarker.appearAnimation = kGMSMarkerAnimationPop
locationMarker.icon = GMSMarker.markerImage(with: UIColor.green)
locationMarker.opacity = 0.85
locationMarker.isFlat = true
locationMarker.snippet = "My Location"
mapView.selectedMarker=locationMarker
}
below line is the answer
mapView.selectedMarker=locationMarker
swift 3
self.mapView.selectedMarker = marker
In the case of swift 3, you can open the snipet usint the selectedMarker
If you are creating the marker in a similar way to:
marker.position = CLLocationCoordinate2D(latitude: 34.1331168, longitude: -118.3550723)
marker.title = "My super place name"
marker.snippet = "Are you looking a place to play? This is your place! "
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = self.mapView
// Below line will shows the infowindow for marker with out tapping on it
[mapView setSelectedMarker:myLocationOptions]; // myLocationOptions is your desired GMSMarker to show Infowindow with out tapping .
Happy Coding :)
mMapView.selectedMarker = marker
GMSMarkerOptions is deprecated. Using this helped me to show info window without tapping-
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
myMapView.selectedMarker = myGMSMarker
}
--> It shows multiple infoWindows without tapping on marker. You can easily customise it.
for i in 0..
let dict = arrNearByPlacesArray.object(at: i) as? NSDictionary ?? [:]
let lat = dict.object(forKey: "latitude") as? String ?? ""
let long = dict.object(forKey: "longitude") as? String ?? ""
let company_id = dict.object(forKey: "company_id") as? String ?? ""
let totaljobs = dict.object(forKey: "totaljobs") as? String ?? ""
let location = CLLocationCoordinate2D(latitude: Double(lat) ?? 0.0, longitude: Double(long) ?? 0.0)
print("location: \(location)")
let marker = GMSMarker()
//marker.icon = UIImage(named: "maps")
let viewData = Bundle.main.loadNibNamed("MarkerXibView", owner: self, options: nil)?.first as! MarkerXibView . //UIView
marker.iconView = viewData . //UIView
marker.position = location
marker.accessibilityLabel = company_id
marker.map = vwGoogleMap
}

Resources