I have used GMSCoordinateBounds. But it zooms in too much. My marker doesn't show on the road because of the zoom level. My marker shows a little bit away from the road. I have used the below code:
let path = GMSMutablePath()
path.add(CLLocationCoordinate2DMake(23.740424444444443, 90.41102222222223))
let bounds = GMSCoordinateBounds(path: path)
self.googleMap.animate(with: GMSCameraUpdate.fit(bounds, withPadding:10.0)
I have solve the problem like bellow
let path = GMSMutablePath()
path.add(CLLocationCoordinate2DMake(23.740424444444443, 90.41102222222223))
let bounds = GMSCoordinateBounds(path: path)
self.googleMap.animate(with: GMSCameraUpdate.fit(bounds))
self.googleMap.setMinZoom(8, maxZoom: 14)
Related
I'm successfully creating a static image using MapboxStatic with the recorded track coordinates as GeoJson data.
I can't figure out how to edit the color of the route painted on the image - it defaults to a black/gray color and I can't find a way to change it.
I realize it must be by editing the GeoJson attributes itself but I have no idea how to it in swift?
Would love to understand how to change the color in some way...
let camera = SnapshotCamera(
lookingAtCenter: CLLocationCoordinate2D(latitude: self.mapView.camera.centerCoordinate.latitude, longitude: self.mapView.camera.centerCoordinate.longitude),
zoomLevel: 12)
let options = SnapshotOptions(
styleURL: self.mapView.styleURL,
camera: camera,
size: self.mapView.bounds.size)
let coordinates = self.viewModel.getTrack().locations?.map { loc -> CLLocationCoordinate2D in
CLLocationCoordinate2D(latitude: loc.latitude, longitude: loc.longitude)
}
let trackLine = MGLPolyline(coordinates: coordinates!, count: UInt(coordinates!.count))
let data = trackLine.geoJSONData(usingEncoding: String.Encoding.utf8.rawValue)
let getJson = GeoJSON(objectString: String(data: data, encoding: .utf8)!)
options.overlays.append(getJson)
You need to use MGLPolylineFeature which allows to control feature attributes.
So instead of
let trackLine = MGLPolyline(coordinates: coordinates!, count: UInt(coordinates!.count))
it should look something like
let trackLine = MGLPolylineFeature(coordinates: coordinates!, count: UInt(coordinates!.count))
polyline.attributes.updateValue("red", forKey: "stroke")
is it possible to animate my marker rotation using MGLSymbolStyleLayer's iconRotation property ? or is there any other way to rotate my marker smoothly ?
I just want to rotate my marker smoothly , as of now the rotation is fine but it is quite snappy. Also i get different bearing for different markers from the server, is there any way i can rotate my marker using MGLPointAnnotation only ?. My use case is that i want different degree rotation for different markers (each of same icon)
let point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: 30.699335, longitude: 76.836422)
let shapeSource = MGLShapeSource(identifier: "marker-source", shape: point, options: nil)
shapeLayer = MGLSymbolStyleLayer(identifier: "marker-style", source: shapeSource)
if let image = UIImage(named: "auto") {
mapView.style?.setImage(image, forName: "auto")
}
shapeLayer?.iconImageName = NSExpression(forConstantValue: "auto")
CATransaction.begin()
CATransaction.setAnimationDuration(3.0)
self.shapeLayer?.iconRotation = NSExpression(forConstantValue: 120.0)
CATransaction.commit()
You can animate an MGLSymbolStyleLayer using the add live data example. You would need to set your #objc function to layer.iconRotation and have that value change every time the function is executed. You can also do the same using an MGLPointFeature or MGLPointAnnotation as a source for an MGLSymbolStyleLayer. Please take a look at the Mapbox Add markers and shapes documentation here to see the different features that are supported.
Im building an app with google maps, and i would like to show a route between 2 static points
I was folling this tutorial but i can't make it yet, for some reason it dont show the route.
I dont want to make an dinamic route i just want it from two point that i´ve define
here's some code
GMSServices.provideAPIKey("MY API KEY")
let camera = GMSCameraPosition.camera(withLatitude: 19.0660043, longitude: -98.12050499999998, zoom: 18.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
var source = CLLocationCoordinate2DMake(19.060914, -98.125935)
var destination = CLLocationCoordinate2DMake(19.1660043, -98.13000)
plx help
You need to create a path first and then use that path to create a polyline
let path = GMSMutablePath()
path.add(source)
path.add(destination)
let polyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.red
polyline.strokeWidth = 3.0
polyline.map = self.map
I'm using GoogleMaps for iOS platform, Swift language.
I draw the path according to the Google Maps documentation:
let path = GMSMutablePath()
path.add(previousLocation)
path.add(currentLocation)
let line = GMSPolyline(path: path)
line.strokeWidth = 5
line.strokeColor = .blue
line.map = mapView
My client complains that the route lines intersections must be smoothly rounded. How can I implement this? Some suggestions?
Thanks!
I found the solution.
My problem was that I was always drawing only the last two points:
let path = GMSMutablePath()
path.add(previousLocation)
path.add(currentLocation)
Now, every time when a new location comes, I clear the map and redraw again the entire route:
mapView.clear()
let path = GMSMutablePath()
for location in locationsArray {
path.add(location)
}
let line = GMSPolyline(path: path)
line.strokeWidth = 5
line.strokeColor = .blue
line.map = mapView
Seems that in this way Google Maps draws the route correctly.
Have a look at SwiftSimplify. It is a library that reduces the number of geopoints in the polyline to make it more smooth and efficient.
This library also smooth out the route. I am using this atm. IVBezierPathRenderer
I'm new in Swift, and I'm trying to use MkMapView. I'd like to change the initial zoom. I searched On the API but I don't find anything.
I find only this, about zoom
mapView.zoomEnabled = true
But it isn't what I looking For.
MKMaps don't have a concept of zoom as a configurable variable. Zoom enabled pertains to user interaction. You'll need to configure something manually by setting a region that encompasses your desired zoom level.
For instance, if I wanted to zoom in on some specific coordinate, I could do:
let coordinate: CLLocationCoordinate2D = // .. populate your center
let latitudinalMeters: CLLocationDistance = 50
let longitudinalMeters: CLLocationDistance = 50
let region = MKCoordinateRegionMakeWithDistance(coordinate, latitudinalMeters, longitudinalMeters)
self.mapView.setRegion(region, animated: false) // Set to yes to animate, you said initial load so I image this won't be visible anyways.
Then, adjust the latitudinal/longitudinal dimensions to meet your desired zoom level.
If you wanted, you could probably create a category that adds zoom and calls this in the background.
From Logan's answer, update for Swift 5+
let coordinate: CLLocationCoordinate2D = // .. populate your center
let latitudinalMeters: CLLocationDistance = 50
let longitudinalMeters: CLLocationDistance = 50
let region = MKCoordinateRegion(center: coordinate, latitudinalMeters: latitudinalMeters, longitudinalMeters: longitudinalMeters)
self.mapView.setRegion(region, animated: false) // Set to yes to animate, you said initial load so I image this won't be visible anyways.