Create Map Pins from XML/Array - ios

I'm currently working on a project which grabs a bunch of traffic-cameras from a XML file and and shows them in the app. I have got my Tableview up and running, working perfectly (click on a cell, opens detailView and shows traffic-image). However, I want to display the cameralocations on a map, so users can press the pin they want to see the camera of (and it sends them to a detailView to show the camera).
I'm not sure how to make this work, any ideas?
I got the longitude and latitude coordinates. Link to the XML file: http://webkamera.vegvesen.no/metadata
It is in norwegian but, lengdegrad = longitude and breddegrad = latitude.
This is what I want to achieve (Photoshop screenshot): https://gyazo.com/93d885606efd6e6369018243b64d47e8
I don't know if this is the right place to ask, but please help if you know something :-)
Thanks in advance

To use the XML data:
Add the XML file to your project and use NSXMLParser to read it.
To create annotations on the map:
Create an annotation class based on MKAnnotationView, something like that:
class Annotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
// optionally add subtitle
init(coordinate: CLLocationCoordinate2D, title: String) {
self.coordinate = coordinate
self.title = title
}
}
To create and add the anotations to the map, for each of the parsed items from your XML file create one annotation and add it to the map. Something like this, depending on format of your parsed data. Let's say you have the data in an array called cameras:
for camera in cameras {
let coord = CLLocationCoordinate2DMake(camera.latitude, camera.longitude)
let annotation = Annotation(coordinate: coord, title: camera.title)
mapView.addAnnotation(annotation)
}

Related

How to get marker data of current camera position in Google Maps iOS SDK?

I'm building a tableview/scrollview to the lower part of a window containing a GMSMapView.map, which is intended to display the marker data of the current camera position.
The GMSMapView.map has already markers set in.
I'm missing the way to get the marker data of the current camera position, checked through Google's documentation but couldn't find it there, any help largely appreciated.
Some digging and here's the answer, simplified, using the SDK's mapView didChange position func, single already defined marker and print confirmations:
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
let camView = mapView.projection.visibleRegion()
let cameraBox = GMSCoordinateBounds(region: camView)
if cameraBox.contains(marker.position) {
print("YES")
}
else {print("NO")
}

iOS: Creating dynamic speech balloons for annotations that appear from user input using MapBox

In the code posted, when you click on the annotation, the speech balloon pops up to say
Hello World!
Welcome to my marker
I would like to know how to make the speech bubble appear while using the app, and have the speech bubble display some text that the user would enter in, and disappear after about an hour or so. The bubble would be able to be seen by other users even if the user logged out or closed the app, and the bubble would still be open when the user goes back into the app, unless the window of time for the bubble has passed.
Thank-you
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Set the map’s center coordinate and zoom level.
mapView.setCenter(CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407), zoomLevel: 12, animated: false)
view.addSubview(mapView)
// Set the delegate property of our map view to `self` after instantiating it.
mapView.delegate = self
// Declare the marker `hello` and set its coordinates, title, and subtitle.
let hello = MGLPointAnnotation()
hello.coordinate = CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407)
hello.title = "Hello world!"
hello.subtitle = "Welcome to my marker"
// Add marker `hello` to the map.
mapView.addAnnotation(hello)
}
// Use the default marker. See also: our view annotation or custom marker examples.
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
return nil
}
// Allow callout view to appear when an annotation is tapped.
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}
}
Are you wanting the user to enter text into a text field inside the annotation bubble? If so, consider subclassing MGLPointAnnotation and adding a text field to it. That might be a bit tricky though since it would appear that MGLPointAnnotation is a subclass of MGLShape, which appears to not be a subclass of the usual UIKit hierarchy of view/view-controller classes. You may be better off swapping out the Mapbox framework for a basic MapKit solution (...I don't know for what else you are relying on Mapbox though).
Apple's MapKit does have MKAnnotationView. There is a definite answer for how to add a UITextField to MKAnnotationView. See how to add UITextField in MKAnnotationView Title. You may need to modify the answer depending on how you want your annotation to behave.
If, on the other hand, you were thinking of the user entering text into a text field through another screen in the iOS app, there are many easy ways to properly implement a UITextField in a UIViewController, UIView, UITableViewController, UICollectionView, etc.
Alternatively, if you were thinking about the user entering text through a website, that is trivially easy with HTML forms.
For the approximate 1 or 3 hour(s) timeframe for displaying the bubble before it goes away, you would need to add a createdTimestamp property to the MKAnnotationView subclass. Just compare the current time periodically to the createdTimestamp on the annotation and if currentTime >= annotation.createTimestamp + oneHour, remove the annotation from the map. You can see about dates in Swift here: https://developer.apple.com/documentation/foundation/date
As far as "other users" seeing the bubble goes, that would require some sort of networked solution (such as a central server that synchronizes with these bubbles' data and then broadcasts them to other users). You would need a networked setup anyway if you were thinking of using website(s) to gather/display map data.
Presumably from your other question, I assume that you are using the map fullscreen. There are several approaches to this.
You could use the default I accessory button to add a target action to it. Which calls a custom UIView which has a textView and a submit button. Which then modifies your annotation.
Or you could modify your mapview to show a small textbox at the bottom of the screen which is then shifted upwards whilst editing and added to your annotation upon submitting.
When it comes to your timeout question I did not find anything in the MapBox's documentation to get you the results you want. I believe it needs some sort of backend server side timer function which will handle this accordingly.

List of neearby searched types IOS xcode

So this piece of code gets nearest searchedTypes(atms and banks) for google and puts markers around the map.
private func fetchNearbyPlaces(coordinate: CLLocationCoordinate2D) {
mapView.clear()
dataProvider.fetchPlacesNearCoordinate(coordinate, radius:searchRadius, types: searchedTypes) { places in
places.forEach {
let marker = PlaceMarker(place: $0)
marker.map = self.mapView
}
}
}
How do I get a table view with the list of these nearest searedTypes and get information on them to show on another viewcontroller where I can navigate from user location to that point. How the normal google maps works.
With places acquired, you can pass data to a new controller that has table view and display data there.

How do I allow users to click on iOS maps to show a callout in swift?

I am trying to build an application where I need to show a callout with details regarding business (name, address etc) when user clicks on the points of interests on the map.
I am able to show callout when there is an annotation. But I want to have a functionality similar to apple maps application, where even without an annotation, users are able to directly tap on the point of interest to show the details about that point of interest.
I have already set the following properties on my mapView:
mapView.userInteractionEnabled = true
mapView.showsPointsOfInterest = true
Any help is appreciated.
you can use MKMapViewDelegate and override Mouse Event
override func rightMouseDown(theEvent: NSEvent) {
let eventLocation: NSPoint = theEvent.locationInWindow
// do something
NSNotificationCenter.defaultCenter().postNotification(notification)
}

Fetch the title of map dropped pin via MKPointAnnotation

I need to know how to fetch the title of a dropped pin. I have multiple dropped pins on the map, when the pin is tabbed I want to fetch the title to pass to prepareForSegue. I use this statement "let title = self.pointAnnotation.title" via (MKPointAnnotation) but I get the title of the last dropped pin and not the tabbed pin.
An easy and reliable way to get a reference to the annotation that was tapped is to use the map view's selectedAnnotations array: if let ann = self.mapView.selectedAnnotations[0] as? MKAnnotation {
println("(ann.title!)")
}

Resources