I have this code in MapView Coordinator class for adding annotation to map:
func mapView(_ mapview: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
return mapview.dequeueReusableAnnotationView(withIdentifier: "myiconid")
}
Annotation added in center of my location :
How can I add annotation above my location?
The documentation says:
By default, the center point of an annotation view is placed at the coordinate point of the associated annotation. You can use this property to reposition the annotation view as needed. This x and y offset values are measured in points. Positive offset values move the annotation view down and to the right, while negative values move it up and to the left.
https://developer.apple.com/documentation/mapkit/mkannotationview/1452144-centeroffset
So you might want to change the centerOffset attribute for your annotations...
Annotation are set on the latitude and longitude that you provide while making it. I think there is no means we can place our annotation above our location unless we change our latitude and longitude.
Related
I've had a look at similar questions but nothing seems to answer this question exactly.
I have a Map view and I want to add pin overlays, as well as a poly line route overlay on top of it. I am very very new at SwiftUI dev (this is my first app), so some help would be appreciated.
At the moment the map only renders the pins, and not the overlay. I'm assuming they need to be put into the same method but cannot figure out how you can return multiple overlays to the mapView.
Here is the Coordinator code:
class Coordinator: NSObject,MKMapViewDelegate{
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
//draw pins
if annotation.isKind(of: MKUserLocation.self){return nil}
else{
let pinAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "PIN_VIEW")
pinAnnotation.tintColor = .red
pinAnnotation.animatesDrop = true
pinAnnotation.canShowCallout = true
return pinAnnotation
}
}
private func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKPolylineRenderer? {
//draw route
let render = MKPolylineRenderer(overlay: overlay)
render.strokeColor = .orange
render.lineWidth = 2
return render;
}
}
Here is the output: (no route is drawn). I tried to put the annotations within the same method but couldn't figure out how to return both at the same time (pins are of type MKAnnotationView and the route is of type MKPolylineRenderer...
Help would be really appreciated. Thanks
It should be a separate method, but it shouldn’t be private. Also, the return type should be MKOverlayRenderer.
I have added clustering with annotations in my project and it's working fine but having problem to set the limit.
Please check below image here, grouping is done for 6 annotations but I want to set clustering limit which should start grouping at 20 (So never see one of these with the number 19 or lower)
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
return MKClusterAnnotation(memberAnnotations: memberAnnotations)
}
Also I tried this but not working for me.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let groupedpin = annotation as! MKClusterAnnotation
if groupedpin.memberAnnotations.count >= 20 { //<— Not working
let view = PlaceClusterAnnotationView.annotationView()
view._count = groupedpin.memberAnnotations.count.description
return view
}
return MKAnnotationView()
}
You cannot force annotations to cluster, but you can prevent annotations from clustering. Do not give any view a clusteringIdentifier (meaning, set it to nil to prevent clustering; an empty string "" still acts as a clustering identifier) until you want it to be able to cluster, and keep the view's displayPriority at required until you want it to be able to cluster. To try to make a view cluster, set its displayPriority to 0. If you suddenly set twenty views' displayPriority to 0 then if they are close enough together they will probably all cluster. But you can't guarantee this.
I want to have the user location being shown as a custom image (not the blue beacon). I also want that whenever the user location changes its coordinates, this custom image is rotated accordingly to represent the user course. In order to achieve that, I implemented the code below. It is being called with annotation type userLocation when the app is started, so that the user location custom image is being shown fine. The problem is that when I simulate a change in user location coordinates, the delegate mapView(_:viewFor:) method is never called again with an annotation type userLocation, so the image is never rotated. (Note: the UIImage rotateImage(by:) is a custom method).
Is there some means to call mapView(_:viewFor:) so that I can update the image annotation for user location when user location changes? Thanks for any help.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isEqual(mapView.userLocation) {
// Annotation image for user location
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
annotationView.image = UIImage(named: "UserLocationImage.png")?.rotateImage(by: currentCourse)
return annotationView
}
}
Use CLLocationManagerDelegate
https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate
Implement this protocol in your code, and put your mapView function into its locationManager(_:didUpdateLocations:) method.
When a user taps an overlay, the following code is triggered:
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
}
I wonder if we can extract the exact LAT and LONG coordinates of the overlay that has been tapped?
Thanks!
To solve this we need the 2 methods together,
So I combined them in a way I hope it will help in this issue:
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
print(overlay)
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
print(coordinate)
for polyline in polylines {
if GMSGeometryIsLocationOnPath(coordinate, polyline.path!, true) {
self.mapView(mapView, didTap: polyline)
}
}
for polygon in polygons {
if GMSGeometryContainsLocation(coordinate, polygon.path!, true) {
self.mapView(mapView, didTap: polygon)
}
}
}
if the user clicked at coordinate we will deal with this, Then check if this coordinate is contained in any Polyline or Polygon we had defined before, So we fire the event didTap overlay for that overlay.
Make sure to make polylines and polygons isTappable = false
And but in mind this event will be fired for every overlay tapped even though if they are overlaped, You can put return when the if success to take the first overlay only
You can use didTapAt delegate method for the same.
#wajih, for you did the method didTapAt was called when you tap on landmark names or titles? For me it's not getting called.
For my case the landmark title is above the polygon and the didTapAt method is not getting called, but if i set tappable to true then didTap(overlay) is called perfectly.
If you just want to get the exact coordinates wherever you tapped whether on Overlay or not, then there is another delegate of GMSMapViewDelegate which is called whenever we tap on GoogleMaps. In this delegate you can get the exact coordinates where you tapped on map irrespective of tapping on an overlay.
Swift 3.0
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
print(coordinate.latitude)
print(coordinate.longitude)
}
If you want to get the coordinate only on tapping marker, then use this delegate method
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print(marker.position.latitude)
print(marker.position.longitude)
return true
}
Make sure to make your overlay as non-tappable
overlay.isTappable = false
For reference see here
I'm using a custom image for MapKit annotations using :
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {}
The "pin" shows with the right image now when I add a new annotation to my map, but it seems to use the horizontal and vertical centre of the image as the pin point. Is it possible to make this the bottom centre of the image? I could create a new image thats double the hight with the bottom being blank, but was hoping I could do this programmatically
Thanks