How to load google map polyline view in tableview cell? - ios

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))

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.

Swift loop through array

I'm trying to loop through an array with coordinates and show markers on a map with this code:
for index in 0...3 {
let latitude: Double = latCoordinate.objectAtIndex(index) as! Double
let longitude: Double = longCoordinate.objectAtIndex(index) as! Double
let position = CLLocationCoordinate2DMake(latitude, longitude)
print([latitude],[longitude])
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = self.mapView
}
But I just get one marker on the map, it should be 4. Why can't I see the other markers on the map?
Your code looks good for displaying the marker, but are you sure the coordinates are correct? - Just looked at the comments again and the issue is in how you're populating your array, if you only have one coordinate in your array then you're only going to see one marker on the map.
This code from my app adds all the coordinates to a GMSCoordinateBounds and then sets the GMSMapView camera to the bounds of all the coordinates - so everything should be visible on the displayed map.
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)
}
let camera = mapView.cameraForBounds(bounds, insets:UIEdgeInsetsZero)
mapView.camera = camera;
}
Hope that helps.

How to set visible region/zoom level for google maps IOS to show all markers added to the mapview

I have used Google maps in ios app, I wanna set zoom level dynamically depends on the search i made over map. Basically am adding pins by searching with city names or lat/long query. after every search am adding pins & i need to show all added markers by the recent search i made.
#IBOutlet weak var mapView: GMSMapView!
let camera = GMSCameraPosition.cameraWithLatitude(23.0793, longitude:
72.4957, zoom: 5)
mapView.camera = camera
mapView.delegate = self
mapView.myLocationEnabled = true
*** arry has dictionary object which has value of Latitude and Longitude. ***
let path = GMSMutablePath()
for i in 0..<arry.count {
let dict = arry[i] as! [String:AnyObject]
let latTemp = dict["latitude"] as! Double
let longTemp = dict["longitude"] as! Double
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latTemp, longitude: longTemp)
marker.title = "Austrilia"
marker.appearAnimation = kGMSMarkerAnimationNone
marker.map = self.mapView
path.addCoordinate(CLLocationCoordinate2DMake(latTemp, longTemp))
}
let bounds = GMSCoordinateBounds(path: path)
self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))
See this answer for a simple way to iterate over a given array of markers and then set the bounds accordingly.

Resources