Changing GMSMarker icon doesn't work - ios

I'm trying to change GMSMarker icon with Moa but marker is still showing default icon. This is my code:
override func viewDidLoad() {
super.viewDidLoad()
locationmanager.delegate = self
let authorizationStatus = CLLocationManager.authorizationStatus()
if(authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways) {
locationmanager.startUpdatingLocation()
MapView.isMyLocationEnabled = true
MapView.settings.myLocationButton = true
} else {
locationmanager.requestWhenInUseAuthorization()
}
MapView.camera = GMSCameraPosition.camera(withLatitude: 32.4279, longitude: 53.6880, zoom: 5)
MapView.delegate = self
prepareMapMarkers()
}
func prepareMapMarkers(){
let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: CLLocationDegrees(malls[0].lat), longitude: CLLocationDegrees(malls[0].long)))
marker.map = MapView
moa.onSuccess = { image in
print(image)
marker.icon = image.images?[0]
return image
}
moa.url = "http://www.w3schools.com/css/img_fjords.jpg"
}
console
<UIImage: 0x60000009e050>, {600, 400}
That image is just for test. Why it's not working?

Try this! It worked for me
let pin = UIImage(named: "maps_icon_location")!.imageWithRenderingMode(.AlwaysTemplate)
marker.icon = pin

You are attempting to change the image of a marker after it is added. I am not sure if Google Maps SDK supports this, but it is not a good idea. Instead, download you photo, then create the marker and add it to the map:
moa.onSuccess = { image in
let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: CLLocationDegrees(malls[0].lat), longitude: CLLocationDegrees(malls[0].long)))
marker.icon = image.images?[0]
marker.map = self.MapView
}
moa.url = "http://www.w3schools.com/css/img_fjords.jpg"

Related

Move Multiple marker with their live location at my end

I am building an application where I have to move multiple marker. These markers will be visible at my end according to the multiple user's live locations. I am able to update only a single marker. but the rest are not updating. I stored these data in firebase and I am getting their data from there.
override func viewDidLoad() {
super.viewDidLoad()
Database.database().reference().child("users").observe(.childChanged, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]
{
self.hideHUD()
self.latitudeFloatValue = (dictionary["lat"] as! Double)
self.longitudeFloatValue = (dictionary["long"] as! Double)
self.moveMent.delegate = self
//set old coordinate
self.oldCoordinate = CLLocationCoordinate2DMake(self.latitudeFloatValue, self.longitudeFloatValue)
print(self.oldCoordinate)
// Create a GMSCameraPosition that tells the map to display the marker
let camera = GMSCameraPosition.camera(withLatitude: self.latitudeFloatValue, longitude: self.longitudeFloatValue , zoom: 14)
self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.mapView.isMyLocationEnabled = true
self.mapView.delegate = self
self.view = self.mapView
// Creates a marker in the center of the map.
self.driverMarker = GMSMarker()
self.driverMarker.position = self.oldCoordinate!
self.driverMarker.icon = UIImage(named: "car")
self.driverMarker.map = self.mapView
self.copyCordinates = self.cordinates
print(self.copyCordinates)
print(self.cordinates)
self.cordinates = ["lattitude":self.latitudeFloatValue,"longitude":self.longitudeFloatValue]
print("self.cordinates",self.cordinates)
print(self.oldCordinates)
print(self.newCordinates)
if(self.oldCordinates["lattitude"] == nil)
{
self.oldCordinates = self.cordinates
self.newCordinates = self.cordinates
}
self.newCordinates = self.cordinates
self.oldCordinates = self.copyCordinates
print("oldCordinates",self.oldCordinates)
print("newCordinates",self.newCordinates)
self.movinfMarker()
}
}, withCancel: nil)
//for use in background
self.locman.requestAlwaysAuthorization()
locman.requestWhenInUseAuthorization()
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways){
guard let currentLocation = locman.location else {
return
}
stringLatitude = currentLocation.coordinate.latitude
stringLongitude = currentLocation.coordinate.longitude
print(currentLocation.coordinate.latitude)
print(currentLocation.coordinate.longitude)
}
// Create a GMSCameraPosition that tells the map to display the marker
let camera = GMSCameraPosition.camera(withLatitude: stringLatitude, longitude: stringLongitude , zoom: 7)
self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.mapView.isMyLocationEnabled = true
self.mapView.delegate = self
self.view = self.mapView
Database.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]
{
self.latitudeFloatValue = (dictionary["lat"] as! Double)
self.longitudeFloatValue = (dictionary["long"] as! Double)
self.titleMarker = (dictionary["username"] as! String)
self.uuidValue = (dictionary["uuid"] as! String)
self.states = [
State(name: self.titleMarker, uuid: self.uuidValue, long: self.longitudeFloatValue, lat: self.latitudeFloatValue),
]
}
for state in self.states {
// Creates a marker in the center of the map.
let state_marker = GMSMarker()
state_marker.position = CLLocationCoordinate2D(latitude: state.lat, longitude: state.long)
state_marker.icon = UIImage(named: "car")
state_marker.title = state.name
state_marker.userData = state.uuid//zIndex = Int32(state.uuid)!
state_marker.snippet = "Hey, this is \(state.name)"
state_marker.map = self.mapView
self.markerDict[state.name] = state_marker
}
}, withCancel: nil)
}
func movinfMarker(){
driverMarker.map = nil
let oldCoodinate: CLLocationCoordinate2D? = CLLocationCoordinate2D(latitude: self.oldCordinates["lattitude"] ?? 0.0 , longitude: self.oldCordinates["longitude"] ?? 0.0)
let newCoodinate: CLLocationCoordinate2D? = CLLocationCoordinate2D(latitude: self.newCordinates["lattitude"] ?? 0.0 , longitude: self.newCordinates["longitude"] ?? 0.0)
print("oldCordinatesNewWala",oldCoodinate as Any)
print("newCordinatesNewWala",newCoodinate as Any)
driverMarker.icon = UIImage(named: "car")
mapView.camera = GMSCameraPosition.camera(withTarget: newCoodinate!, zoom: 17)
driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
driverMarker.position = oldCoodinate ?? CLLocationCoordinate2D(latitude: 0.0,longitude: 0.0)
//this can be old position to make car movement to new position
driverMarker.map = mapView
//marker movement animation
CATransaction.begin()
CATransaction.setValue(Int(2.0), forKey: kCATransactionAnimationDuration)
CATransaction.setCompletionBlock({() -> Void in
self.driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
//New bearing value from backend after car movement is done
})
driverMarker.position = newCoodinate ?? CLLocationCoordinate2D(latitude: 0.0,longitude: 0.0)
//this can be new position after car moved from old position to new position with animation
driverMarker.map = mapView
driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
//found bearing value by calculation
CATransaction.commit()
}

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

Google Maps not zooming to my current location

I have to plot multiple pins in a Google Map view, and then zoom to the user's current location. Pins are plotted successfully but not zoomed to current location. Any help would be appreciated
override func viewDidAppear(_ animated: Bool) {
if (self.googleMapsView == nil) {
self.googleMapsView = GMSMapView(frame: self.mapViewContainer.frame)
self.view.addSubview(self.googleMapsView)
print(AppDelegate.getAppDelegate().mapArray)
googleMapsView.clear()
for i in 0...AppDelegate.getAppDelegate().mapArray.count - 1 {
let lon = Double ((AppDelegate.getAppDelegate().mapArray[i]["lon"] as? String)!)
let lat = Double ((AppDelegate.getAppDelegate().mapArray[i]["lat"] as? String)!)
let tit = AppDelegate.getAppDelegate().mapArray[i]["location"] as? String
locateWithLongitude(lon: lon!,andLatitude: lat! ,andTitle: tit!)
}
let marker = GMSMarker(position: AppDelegate.getAppDelegate().myLocation)
let camera = GMSCameraPosition.camera(withLatitude: AppDelegate.getAppDelegate().myLocation.latitude, longitude: AppDelegate.getAppDelegate().myLocation.longitude, zoom: 11)
self.googleMapsView.camera = camera
marker.title = AppDelegate.getAppDelegate().sublocality
marker.map = self.googleMapsView
self.googleMapsView.animate(to: camera)
}
}
func locateWithLongitude(lon: Double, andLatitude lat: Double, andTitle title: String) {
DispatchQueue.main.async() { () -> Void in
let position = CLLocationCoordinate2DMake(lat, lon)
let marker = GMSMarker(position: position)
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 0)
self.googleMapsView.camera = camera
marker.title = title
marker.map = self.googleMapsView
}
}
Your zoom is set to 0, increase it to 15/16 like bellow.
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 16)

Ios Google maps keep current location course(angle) always north like google Navigation

I'm using google maps to track
func ShowcurrentMarker()
{
currentlocationCoordinate = CLLocationCoordinate2DMake(LatestLocation.coordinate.latitude, LatestLocation.coordinate.longitude)
if(CurrentLocationMarker == nil)
{
CurrentLocationMarker = GMSMarker(position:currentlocationCoordinate)
CurrentLocationMarker.map = self.mapView
CurrentLocationMarker.icon = UIImage.init(named:"car_icon")
mapView.camera = GMSCameraPosition.camera(withTarget: CLLocationCoordinate2DMake(LatestLocation.coordinate.latitude, LatestLocation.coordinate.longitude), zoom: 15.0)
}
else
{
CATransaction.begin()
CATransaction.setAnimationDuration(2)
CurrentLocationMarker.position = currentlocationCoordinate
CurrentLocationMarker.rotation = CLLocationDirection.abs(LatestLocation.course)
mapView.camera = GMSCameraPosition.camera(withTarget: CLLocationCoordinate2DMake(LatestLocation.coordinate.latitude, LatestLocation.coordinate.longitude), zoom:mapView.camera.zoom)
CATransaction.commit()
}
}
Here is my working code. Now I want my car always to keep on north facing like navigation app.
How can I do that ?

How change location arrow icon in google maps in swift code?

this is my code and I want to change location arrow icon (blue dot). how I can do it in swift code. I'm using google maps! I attached photo which is I want to do it.
func moveCamera(marker:GMSMarker){
map.camera = GMSCameraPosition(target: marker.position, zoom: 12, bearing: 0, viewingAngle: 0)
}
func placeMarkersInMap(){
var marker:GMSMarker = GMSMarker()
if(addressArray.count > 0){
for add in addressArray{
let position = CLLocationCoordinate2DMake(add.longitud, add.latitude)
marker = GMSMarker(position: position)
marker.appearAnimation = kGMSMarkerAnimationPop
marker.icon = UIImage(named: "marker")
marker.title = add.location
marker.snippet = "\nTel. \(add.tel)"
marker.infoWindowAnchor = CGPointMake(0.44, 0.45)
marker.map = map
}
moveCamera(marker)
}
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
map.myLocationEnabled = true
map.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
locationManager.stopUpdatingLocation()
}
}
let position = CLLocationCoordinate2DMake(// here will be your coords)
let london = GMSMarker(position: position)
london.title = "London"
london.icon = UIImage(named: "here will be your image name")
london.map = mapView;

Resources