I queried a set of coordinates for users around me from Parse(PFGeoPoint) and I have created annotations for said locations/coordinates. I will be updating the users location every 5 seconds and updating the location of the annotations, everything is working fine except for the fact that I'm having to remove and add annotations instead of them moving on their own similar to lyft, is there a way to do this in swift? please reply, thank you!!
Yes it is. As I can see from your tags, you are using MKPointAnnotation , so you an access its coordinate property and update it as you wish.
var coordinate: CLLocationCoordinate2D
So all you have to do is set it up like this:
annotation.coordinate = newCoordinate
For more information, please look at Accessing the Annotation’s Location.
Related
I'm coding an iOS app with a Mapbox MGLMapView displayed on my view controller, on this mapView I draw a route using a MGLShapeSource, MGLLineStyleLayer and MGLPolylineFeature objects.
Here is the code :
let newSource = MGLShapeSource(identifier: "polylineBlue", shape: self.polylines, options: nil)
mapView.style?.addSource(newSource)
let newStyle = MGLLineStyleLayer(identifier: "polylineBlueLayer", source: source)
...styling my layer...
mapView.style?.addLayer(newStyle)
source.shape = self.polylines // a MGLPolylineFeature object
Works great for the route but there is one issue : is appears on top of my annotations.
I add the annotation with regular mapView function :
mapView.addAnnotations([..my MGLPointAnnotation objects...])
I've tried searching here and other websites, I only found one topic and there is nothing helpful except someone saying that we can't set a z layer position on annotations so no fix for that.
Does someone know a workaround ? Do I have to use that : https://docs.mapbox.com/ios/maps/examples/add-marker-symbol/ ?
if so, do I need to create one MGLSymbolStyleLayer per annotation ?
Sounds like a painful solution for a so basic need...
Thanks !
In the code snippets that you provided, it appears that your annotations are MGLPointAnnotations. If this is the case, you would need to add your MGLPointAnnotations to an MGLShapeSource using MGLShapeSource(initWithIdentifier:shapes:) and then use this shape source to create your MGLSymbolStyleLayer.
To ensure that the annotations show on top of your route, you will need to verify when each layer is being added, as layers are “baked” into the map before rendering. If you add the MGLSymbolStyleLayer responsible for the annotations after the route is added to the map, they will appear on top. If you add them before the route loads, they will appear below the route line layer. Only one MGLSymbolStyleLayer is needed.
For additional information on markers and annotations, please take a look at Mapbox’s documentation here.
I'm a little new to Objective C and iOS programming, I've been exploring MKMapView recently, and I've been able to add annotations successfully.
[self.mapView addAnnotations:annotations]
However,
when I try to add an overlay, I'm running into issues,
I add the overlay and then I need to add setVisibileMapRect.
When I write something like this-
[self.mapView addOverlay:MyOverlayObject]
BOOL mapContainsOverlay = MKMapRectContainsRect(self.mapView.visibleMapRect,
[MyOverlayObject boundingMapRect]);
if (mapContainsOverlay) {
//This is not getting executed.
}
NSLog(#" %hhd", mapContainsOverlay) //Prints 0
Any Idea why mapContainsOverlay is 0?
If the map is looking at England, and the overlay is in New Zealand then self.mapView.visibleMapRect does not contain [MyOverlayObject boundingMapRect] and thus MKMapRectContainsRect returns false (which is printed as 0). I don't know what your rects are but I'm guessing that the rect2 doesn't fit "entirely within rect1" as per the spec. e.g. France does not contain Europe.
If you want to ensure that your map is looking at where the overlay you need to set the visible map rect.
Simple Question: Seems I cannot find the tag attribute for MKPointAnnotation class. It return error;
MKPointAnnotation *annotation = [[HCIAnnotationViewController alloc]
initwithHouse:house];
The following returns error (Property tag not found ob object of type "MKPointAnnotation"
NSLog(#"%d",annotation.tag);
My question is, if Im not allowed to set the tag, How am I supposed to detect which annotation was clicked.
The other approaches I followed are
Setting tag for MkAnnotationView, However in this what I found out is that the last annotation when added doesn't immediately call for viewForAnnotation (Might be because I'm adding around 1000 MkPointAnnotations to a small map, so it only calls when it comes to view.).
Please tell me how to resolve this?
Since MKPointAnnotation is not a subclass of UIView, has not a property called tag. Bu you have the property coordinate. I assume you have different coordinates for all of your annotations. So you can detect which one.
You just need to compare the coordinates.
Not very well versed with iOS Development. I have a very stupid question to ask!! is there a difference between Placemarks and Annotations. Ive been through some Apple Developer documents. Somewhere it was mentioned Placemarks have areaOfInterest property. Im not sure if annotations have one too.
Apart from that how do Placemarks differ from Annotations??
Thanks in Advance!! Cheers
Annotations offer a way to highlight specific coordinates on the map and provide additional information about them. You can use annotations to call out specific addresses, points of interest, and other types of destinations. When displayed on a map, annotations typically have some sort of image to identify their location and may also have a callout bubble providing information and links to more content.
For Placemarks See This Link
http://ioscreator.com/display-placemarks-with-mapkit/
From the MKPlacemark class reference:
A placemark is also an annotation and conforms to the MKAnnotation protocol, whose properties and methods include the placemark coordinate and other information. Because they are annotations, you can add them directly to the map view.
MKPlacemark inherits from CLPlacemark, which has property areasOfInterest.
A look at the class references for both classes and the protocol should clear things up.
I am working on mapview project. I have question related to removing annotation from the mapView.
I have implemented the following code but it removes annotation randomly, not the first one!
[mapView removeAnnotation:[self.mapView.annotations objectAtIndex:0]];
self.mapView.annotations gives you an NSArray of all the annotations, but it does not promise to be in the same order each time nor in the order you added annotations. You will have to find another way to get a reference to the one you intend to delete.
I have came across a solution. Whomever concerns about this question's answer, there you go
I have created an NSMUtable array and add all objects into array then remove first element from the mapview
[mapView removeAnnotation:[annotationArray objectAtIndex:0]];