how can in a `enter code here map-kit move to MKPointAnnotation old let long to new let long with animation with move Move With Effect in swift
used this code
animation with also draw a polyline
let startPosition = CLLocationCoordinate2D(latitude: 23.0118, longitude: 72.5055)
let destinationPosition = CLLocationCoordinate2D(latitude: 23.0133, longitude: 72.5308)
// Set the region the MapView will be looking at at.
let region = MKCoordinateRegionMakeWithDistance(startPosition, 100000, 100000)
mapView.setRegion(region, animated: true)
moveDelivery(CLLocationCoordinate2D(latitude: 23.0133, longitude: 72.5308))
allLocations.append(startPosition)
// Add annotation to map.
myAnnotation.coordinate = startPosition
mapView.addAnnotation(myAnnotation)
` weak var timer: Timer?
func movePosition() {
// Set timer to run after 5 seconds.
timer = Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { [self] _ in
// Set animation to last 4 seconds.
UIView.animate(withDuration: 10, animations: { // Update annotation coordinate to be the destination coordinate
self.myAnnotation.coordinate = destinationPosition
}, completion: nil)
}
}
// Start moving annotation.
movePosition()
`
used than MKPointAnnotation Move but I cant draw Polyline parallel in swift
Related
I'm trying to zoom google map on particular pin from lat long.it working fine but i want to change pin image when zoom in on pin in swift.i have doing like this but there have putting 2 image on same lat long.
func zoom(lat: Double, long : Double){
CATransaction.begin()
CATransaction.setValue(1, forKey: kCATransactionAnimationDuration)
// It will animate your camera to the specified lat and long
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 15)
self.mapView!.animate(to: camera)
let position = CLLocationCoordinate2D(latitude: lat,longitude: long)
let marker = GMSMarker()
marker.map = self.mapView
marker.icon = UIImage.init(named: "pin-1")
CATransaction.commit()
}
You can put a function that detects changes in your zoom level then put a condition that will change the value of your marker icon.
Here is how my code looks like:
import UIKit
import GoogleMaps
class ViewController: UIViewController, GMSMapViewDelegate {
let marker = GMSMarker()
override func viewDidLoad() {
}
override func loadView() {
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = mapView
mapView.delegate = self
// Creates a marker in the coordinate of the map.
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
marker.icon = UIImage(named: "pin_orange")
}
//This detect the changes in the cameraposition
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
let zoom = mapView.camera.zoom
print("map zoom is ",String(zoom))
//put a condition here to change the icon of your marker
if zoom > 6 {
marker.icon = UIImage(named: "icon1")
}else{
marker.icon = UIImage(named: "icon2")
}
}
}
Hope this helps!
Edit: I found a solution. MKMapView won't show the entire globe if you build it in Xcode's simulator, but it will if you build it on an actual device.
Existing versions of the question haven't worked - Need Swift 4, Xcode 10 solution:
I'm trying to zoom an MKMapView out to show the entire globe in my app. I can zoom out to see a map of the whole world, but not the entire globe.
Here's what I know:
-It has to be satellite, hybrid, satelliteFlyover, or hybridFlyover
-3D must be enabled.
Here is what I've tried:
-Adjusting the span of the region (no matter how big I make the span, it never zooms out enough to see the globe)
-Adjusting the altitude of the camera (no matter how high I make it, it never zooms out enough to see the globe)
I have googled and stack-overflowed, and I can't find any solutions that work. I am reduced to posting this question myself, violating my lurker principals, but I am totally stuck.
Here is some code I've tried from tutorials, etc. (several attempts commented out):
import UIKit
import MapKit
class ViewController: UIViewController {
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
mapView.mapType = .standard
mapView.isPitchEnabled = true
// mapView.showsBuildings = true // displays buildings
let eiffelTowerCoordinates = CLLocationCoordinate2DMake(48.85815, 2.29452)
mapView.region = MKCoordinateRegion(center: eiffelTowerCoordinates, latitudinalMeters: 1000, longitudinalMeters: 100) // sets the visible region of the map
// create a 3D Camera
let mapCamera = MKMapCamera()
mapCamera.centerCoordinate = eiffelTowerCoordinates
mapCamera.pitch = 45
mapCamera.altitude = 50000000 // example altitude
mapCamera.heading = 45
// set the camera property
mapView.camera = mapCamera
// var ausCenter = CLLocationCoordinate2D(latitude: -25.354917, longitude: 134.347407)
// let camera = MKMapCamera(lookingAtCenter: ausCenter, fromDistance: 300, pitch: 10, heading: 0)
// mapView.camera = camera
// var ausCenter = CLLocationCoordinate2D(latitude: -25.354917, longitude: 134.347407)
// var ausSpan = MKCoordinateSpan(latitudeDelta: 5, longitudeDelta: 5)
// var region = MKCoordinateRegion(center: ausCenter, span: ausSpan)
// mapView.setRegion(region, animated: true)
// let camera = MKMapCamera()
// camera.centerCoordinate = mapView.centerCoordinate
// camera.pitch = 90.0
// camera.altitude = 30000000
// camera.heading = 0
// mapView.setCamera(camera, animated: true)
// var ausCenter = CLLocationCoordinate2D(latitude: -25.354917, longitude: 134.347407)
//
//// mapView.setCenter(ausCenter, animated: true)
//
// let span = MKCoordinateSpan(latitudeDelta: 150, longitudeDelta: 150)
// let region = MKCoordinateRegion(center: ausCenter, span: span)
// mapView.setRegion(region, animated: true)
//
// //add an annotation
// let annotation = MKPointAnnotation()
// annotation.coordinate = ausCenter
// annotation.title = "Australia"
// annotation.subtitle = "Details Forthcoming"
// mapView.addAnnotation(annotation)
}
}
I would like to change marker position if button tapped. I tried like below. but it will look like this. Some how color will be changed to like so.
How can I fix this? Thank you!
#objc func changeMarker() {
let next = CLLocationCoordinate2DMake(40.730610, -73.935242)
mapView.camera = GMSCameraPosition.camera(withLatitude: next.latitude, longitude: next.longitude, zoom: 13)
let marker = GMSMarker(position: next)
marker.title = "over here"
marker.map = mapView
}
Add this marker and try this
func addMarker() {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(yourCoordinateLatitude), longitude: CLLocationDegrees(yourCoordinateLongitude))
marker.map = mapView
}
Now you can zoom to your marker
func zoomToCoordinate(coordinate: CLLocationCoordinate2D, zoom: Float) {
CATransaction.begin()
CATransaction.setValue(1, forKey: kCATransactionAnimationDuration)
let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: zoom)
self.animate(to: camera)
CATransaction.commit()
}
I would call it like this
mapView.clear() // to clear your mapView
mapView.addMarker()
mapView.zoomToCoordinate(coordinate: yourCoordinate, zoom: 15)
Always check if your function is called and if the coordinations are correct.
This code works in my project. If it doesn't work for your project please share more code from you.
I have created code that produces a geotargeting area represented by an annotation and MKCircle. The app notifies the user when they have entered and exited a region. Everything is working fine but I cannot figure out how to get the app to hold/display multiple regions (only one annotation/circle will show) here are a few snippets of my code:
override func viewDidLoad() {
super.viewDidLoad( )
//setup locationManager
locationManager.delegate = self
locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
//setup mapView
mapView.delegate = self
mapView.showsUserLocation = true
mapView.userTrackingMode = .Follow
//setup test data will need to link coredata to pass in (LocationLabel, radius, address)
setupData("Test1", radius: 100, Address: "735 Main Rd, Clemson")
setupData("Test2", radius: 100, Address: "821 Main Rd, Clemson")
setupData("Test3", radius: 100, Address: "720 Main Rd, Clemson")
}
func setupData( Label: String, radius: Double, Address: String ) {
// check if system can monitor regions
if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self) {
//region data need to put in its own class to read multiple regions
let title = Label
let regionRadius = radius // in meters
let address = Address // street, city, state zip
//takes in the address of a location and converts it into 2d coordinates (lat/long)
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
if let placemarks = placemarks {
if placemarks.count != 0 {
let coordinates = placemarks.first!.location
let coordinate = coordinates?.coordinate
//setup region this will read an object with a saved coordinate and name
var region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate!.latitude,
longitude: coordinate!.longitude), radius: regionRadius, identifier: title)
self.locationManager.startMonitoringForRegion(region)
//setup annotation
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate!;
annotation.title = "\(title)";
self.mapView.addAnnotation(annotation)
//setup circle
let circle = MKCircle(centerCoordinate: coordinate!, radius: regionRadius)
self.mapView.addOverlay(circle)
}
else {
print("System can't track regions")
}
}
}
}
}
You have to implement the renderForOverlay function of MKMapViewDelegate to actually see those overlays that you have added. Try to increase your radius as well if you want it to see right away without zooming in.
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
{
if let overlay = overlay as? MKCircle
{
let circleRenderer = MKCircleRenderer(circle: overlay)
circleRenderer.fillColor = UIColor.blueColor()
return circleRenderer
}
return MKOverlayRenderer(overlay: overlay)
}
I have a zoomed in map. When user picks a new poi I would like to zoom out (animated) and after animation zoom in to the new poi.
However, it's only zooming out, not in. If I use animated:false when zooming out it is working.
How to zoom in when the map is done zoomed out animated?
func centerMapOnLocation(location: CLLocation) {
//Är kartan inzoomad.. zooma ut först.
if isZoomed
{
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
20000, 20000)
OverviewMap.setRegion(coordinateRegion, animated: false)
}
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 4.0, regionRadius * 4.0)
OverviewMap.setRegion(coordinateRegion, animated: true)
isZoomed=true
}
This code should do what you want. didSelectAnnotationView fires when the user taps on a pin.
var zoomingIn = false
var zoomingAnnotation:MKAnnotation
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView)
{
let pin = view as! MKPinAnnotationView
zoomInOnPin(pin.annotation!)
}
func zoomInOnPin(annotation:MKAnnotation) {
let zoomOutRegion = MKCoordinateRegion(center: mapView.region.center, span: MKCoordinateSpan(latitudeDelta: 0.09, longitudeDelta: 0.09))
zoomingIn = true
zoomingAnnotation = annotation
mapView.setRegion(zoomOutRegion, animated: true)
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if let annotation = zoomingAnnotation where zoomingIn == true {
zoomingIn = false
let region = MKCoordinateRegion(center: zoomingAnnotation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.07, longitudeDelta: 0.07))
mapView.setRegion(region, animated: true)
}
}
You need to make sure one setRegion animation has completed prior to calling the next setRegion.
look into the MKmapViewDelegate regionDidChangeAnimated method. This will allow you to react to the completion of the setRegion animated and chain the next setRegion animation.