how to add annotations in map view? only show one pin - ios

I have 20 cities and store their name, latitude, and longitude in 3 arrays.
here is my code in ViewDidLoad():
var annotations:Array<MKPointAnnotation>=Array(repeating: MKPointAnnotation(), count: cityName.count)
for i in 0 ..< annotations.count{
annotations[i].title=cityName[i]
annotations[i].coordinate.latitude=cityLatitude[i]
annotations[i].coordinate.longitude=cityLongitude[i]
mapView.addAnnotation(annotations[i])
}
however, it only shows one pin on the map which is the last city in the array.
I tried to add the following code which I found in Stack Overflow.
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
print("viewForAnnotation \(annotation.title)")
if annotation is MKUserLocation {
return nil
}
let reuseID = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseID) as? MKPinAnnotationView
if(pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
return pinView
}
and add the MKMapViewDelegate in the file.
But the result is still one pin on the map.
how can I add all of the pins to the map? thanks

Related

Change Pin's color in MapKit - Swift 4 - Xcode 10 - iOS 12

I am trying to change the color of the pin used in my map in MapKit in Xcode. I found this in the Apple Developer Documentation:
Declaration (iOS, tvOS):
var pinTintColor: UIColor! { get set }
I am new to code and I can't figure out what I should put in get and set, could you please help me?
Thank you!
Try the following code:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.pinTintColor = .purple
}
else {
pinView?.annotation = annotation
}
return pinView
}

How to programmatically set the "selected with callout" state for a MapKit marker pin

In 2017 MKMarkerAnnotationView was announced to replace MKPinAnnotationView. As described by this WWDC video at 12:21, MKMarkerAnnotationView has three states:
Normal
Selected
Selected with Callout
How do you programmatically set the "Selected with Callout" state so that it displays as it does in the WWDC video? This seems like it should be a super straight forward thing to do, but I see absolutely nothing in the MapKit documentation, the only way I can get it to work reliably is this:
Documentation Links:
MKAnnotationView
MKMarkerAnnotationView
You can use canShowCallout property.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKMarkerAnnotationView
if pinView == nil {
pinView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.canShowCallout = true
let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
pinView?.rightCalloutAccessoryView = rightButton as? UIView
}
else {
pinView?.annotation = annotation
}
return pinView
}
And you need to select the annotation to set the "Selected with Callout" state.
mapView.selectAnnotation(annotation, animated: true)

Autosize annotation.title [duplicate]

I have a mapView with annotations displaying titles and subtitles. The subtitles are sometimes longer than the width of the annotation, so I am wondering if i can make them multiline?
It's coded like this so far:
func annotate(newCoordinate, title: String, subtitle: String) {
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = title
annotation.subtitle = subtitle
self.map.addAnnotation(annotation)
}
Then i have a few options set in
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {...}
which are not relevant here.
Is it posible to make a custom annotation view?
I've tried a couple of things, but nothing worked. The closest I can get is adding a button to display the longer subtitle separately, but i'd rather have it inside the annotation.
Is it possible?
I figured it out, I added a label in viewForAnnotation and it just worked
¯\_(ツ)_/¯
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
}
else {
pinView!.annotation = annotation
}
//THIS IS THE GOOD BIT
let subtitleView = UILabel()
subtitleView.font = subtitleView.font.fontWithSize(12)
subtitleView.numberOfLines = 0
subtitleView.text = annotation.subtitle!
pinView!.detailCalloutAccessoryView = subtitleView
return pinView
}

Why can't I customize my MKAnnotationView?

In my code I am using Apple maps and correctly setting up a mapView and the delegate to self within a View Controller.
Within my ViewController I also have the following code within a for loop:
if userOnMapAlready == false {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: otherUserLocation.coordinate.latitude, longitude: otherUserLocation.coordinate.longitude)
annotation.subtitle = otherUserUUID
self.mapView.addAnnotation(annotation)
}
I also have the following delegate function in my view controller, and again am setting mapview.delegate = self within the View Controller:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .purple
}
else {
pinView!.annotation = annotation
}
return pinView
}
Sadly when the pins are drawn they are all red, not purple like the viewForAnnotation would suggest they should be. What is the problem?! Is it something to do with the reuseId not matching the unique subtitle im a assigning to each MKPointAnnotation??
UPDATE: just realized that the delegate function viewForAnnotation is never being called - which would explain why the pins are not purple. Why would viewForAnnotation not be getting called???
In the latest version of Swift the property would be:
pinView!.pinTintColor = .purple
or...
pinView!.pinTintColor = MKPinAnnotationView.purplePinColor()
↳ https://developer.apple.com/reference/mapkit/mkpinannotationview

Xamarin.iOS : How to change the annotation pin colour in apple maps

I want to change the colour of pin. Following is my code to add the pin on map. Thanks in advance. Any kind of help will be appreciated:
map.AddAnnotations(new MKPointAnnotation
{
Title = "Technician's Location",
Coordinate = new CLLocationCoordinate2D(lattitude, longitude)
});
You can use the pinColor property of MKPinAnnotationView.
This can be added in the delegate method func mapView(mapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
A typical example of what this method can hold is :-
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.animatesDrop = true
pinView!.pinColor = .Purple // you can change color here!
}
else {
pinView!.annotation = annotation
}
return pinView

Resources