Retrieving city name for onscreen region [Mapbox] - ios

I'm relatively new to iOS and Mapbox development. I'm working on an app where a user can freely manipulate a map full of places they have saved.
When they reach a zoom-level that is completely filled by the geography of a city, I would like to display the name of the city which they are viewing in a banner-style view, even if a city label is not within view on the map (as is often the case when zoomed in).
Here's a screenshot of the UI for context.
I'm trying to query the Mapbox tileset for the city name using the following code:
func mapViewRegionIsChanging(_ mapView: MGLMapView) {
let zoomLevel = mapView.zoomLevel
if zoomLevel >= 14.0 {
// layer identifier taken from layer name in Mapbox Studio
let layerIdentifier = "place-city-lg-n"
let screenRect = UIScreen.main.bounds
let cityName = mapView.visibleFeatures(in: screenRect, styleLayerIdentifiers: Set([layerIdentifier]))
print(cityName)
}
I think this code doesn't work because the label is not onscreen at the specified zoom level.
I'm wondering if using visibleFeaturesInRect is the best approach for my need—is there a better way to retrieve city name regardless of visible elements and zoom level?

For this task I'd recommend using MapboxGeocoder from Mapbox. It is for getting information about the city/village.
you can install pod:
pod 'MapboxGeocoder.swift', '~> 0.12'
and use this code:
let geocoder = Geocoder.shared
func mapViewRegionIsChanging(_ mapView: MGLMapView) {
let geocodeOptions = ReverseGeocodeOptions(coordinate: mapView.centerCoordinate)
geocodeOptions.allowedScopes = [.place]
let _ = geocoder.geocode(geocodeOptions) { (placemarks, attribution, error) in
guard let placemark = placemarks?.first else { return }
print(placemark.name)
print(placemark.qualifiedName)
}
}
you can add your conditions and it really helps to solve your task

Related

Swift MKMapView.setRegion not working after MKLocalSearch.Request

I've been working on an app to annotate an MKMapView with all nightlife locations near a user. I have implemented a recenter function that is called by a SwiftUI button. The recenter works fine based on CLLocationManager.location, but after any MKLocalSearch query, the setRegion stops working. It must be the setRegion because the correct lat/long are still printed. I also know it is not due to the annotations because the same bug happens when the annotation add/remove part is commented out. Does it possibly have something to do with linking the searchRequest.region and map.region? manager is the CLLocationManager instance, map is the MapView
func recenter(){
print("Recenter called")
guard let center = manager.location?.coordinate else{
print("Could not get location for recenter")
return
}
let newRegion = MKCoordinateRegion(center: center, latitudinalMeters: 1000, longitudinalMeters: 1000)
print("\(center.latitude) and \(center.longitude)")
map.setRegion(newRegion, animated: true)
}
func queryAndAnnotate(){
let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = "nightlife"
searchRequest.region = map.region
let search = MKLocalSearch(request: searchRequest)
search.start{response, error in
guard let response = response else {
Alert(title:Text("Alert"), message: Text("Error: \(error?.localizedDescription ?? "Unknown Error")"))
return
}
var mapItemPlacemarks: [MKPointAnnotation] = []
self.barsList = []
for i in response.mapItems{
let x = MKPointAnnotation()
x.coordinate = i.placemark.coordinate
x.title = i.name
mapItemPlacemarks.append(x)
}
self.map.removeAnnotations(mapItemPlacemarks)
self.map.addAnnotations(mapItemPlacemarks)
}
}
MKLocalSearch.start calls the completionHandler in background task.
In this handler you are updating your mapView instance in background task.
Make sure to call all update methods of mapView in main task (removeAnnotations, addAnnotations, setRegion)
btw in
self.map.removeAnnotations(mapItemPlacemarks)
self.map.addAnnotations(mapItemPlacemarks)
you are removing mapItemPlacemarks that do not exist in the mapView. This makes no sense, but it is not your problem.
As it turns out the problem was coming from a dynamic update to a ScrollView. The ScrollView was within a slide up drawer that sat above the MapView on the Z axis. When the ScrollView was updated with the results from the local search, the MapView object lost track of userLocation (read 0,0) and the region could not be programmatically changed. I ended up fixing the problem by ditching the recenter() function altogether and instead implementing an MKUserTrackingButton within its own UIViewRepresentable (for SwiftUI). The MKUserTrackingButton is how the system maps cycles through user tracking modes. You can read the docs for it here: https://developer.apple.com/documentation/mapkit/mkusertrackingbutton

iOS MapKit Get Places information and Reviews

I'm using mapkit and able to get Address for a particular location
let firstPlacemark = placemarks,
let addressDictionary = firstPlacemark.addressDictionary else { return }
// let street = addressDictionary["Street"]
// let city = addressDictionary["City"]
// let state = addressDictionary["State"]
// let zip = addressDictionary["ZIP"]
print(addressDictionary.description)
if let array = addressDictionary["FormattedAddressLines"] as? [Any] {
let address = array.map { "\($0)" }.joined(separator: ",\n")
print("Address : \(address)")
}
But Can we get more information like reviews contact no. images etc. Like Apple Maps shows (Observe Screeenshot)
Is there any API available in iOS for this or it will be provided via TripAdvisor???
P.S: I don't want to Any Google API.
Dear down voters if don't know the answers then kindly don't waste my points. It's easy to downvote, but you never know how much efforts has been added to earn those points.
OR Kindly provide me solution or show me if question is duplicate.
You can use Tripadvisor API
https://developer-tripadvisor.com/content-api/documentation/
ex.
http://api.tripadvisor.com/api/partner/2.0/location/155507?key=YOURKEY

Mapbox Geocoder focalLocation not returning expected results of nearby places

I posted this issue on GitHub, though it has been over a week and no response from the developers, so hoping to get an answer here.
Using the example code, plus adding a bit to show placemarks returned from ForwardGeocodeOptions, I came up with this testing code:
(Swift 3, Xcode 8)
func mapView(_ mapView: MGLMapView, regionDidChangeAnimated animated: Bool) {
geocodingDataTask?.cancel()
self.outputText.text = ""
// Variables.userLat and Variables.userLng are set through locationManager
let options = ReverseGeocodeOptions(coordinate: CLLocationCoordinate2D(latitude: Variables.userLat, longitude: Variables.userLng))
geocodingDataTask = geocoder.geocode(options) { [unowned self] (placemarks, attribution, error) in
if let error = error {
NSLog("%#", error)
} else if let placemarks = placemarks, !placemarks.isEmpty {
self.resultsLabel.text = placemarks[0].qualifiedName
let foptions = ForwardGeocodeOptions(query: self.inputText.text!)
// To refine the search, you can set various properties on the options object.
foptions.allowedISOCountryCodes = ["US"]
foptions.focalLocation = CLLocation(latitude: Variables.userLat, longitude: Variables.userLng)
let neLat = Variables.userLat + 1.0
let neLng = Variables.userLng + 1.0
foptions.allowedRegion?.northEast = CLLocationCoordinate2D(latitude: neLat, longitude: neLng)
let swLat = Variables.userLat - 1.0
let swLng = Variables.userLng - 1.0
foptions.allowedRegion?.southWest = CLLocationCoordinate2D(latitude: swLat, longitude: swLng)
foptions.allowedScopes = [.address, .pointOfInterest]
let _ = geocoder.geocode(foptions) { (placemarks, attribution, error) in
guard let placemark = placemarks?.first else {
return
}
let coordinate = placemark.location.coordinate
print("\(coordinate.latitude), \(coordinate.longitude)")
self.inputLat.text = coordinate.latitude.description
self.inputLng.text = coordinate.longitude.description
var string = ""
for mark in placemarks! {
if string != "" {
string += "\n"
}
string += mark.qualifiedName
}
self.outputText.text = string
}
} else {
self.resultsLabel.text = "No results"
}
}
}
That gives me a mini-app to test out the data that is returned when I change locations in the Xcode Simulator.
screenshot 2017-07-12 13 54 09
As you can see from this shot, I have centered the map in Jenks, OK (a small town just outside of Tulsa, OK - sort of a 'central US' location.)
When searching for a common place in that area ("Walmart" - which is based in nearby Arkansas, so there are plenty of them around), you can see that only 2 'local' Walmart's come back in the search.
Now, let's move to Bentonville, AR - the home of Walmart......
And, we get two top new results, but the others are the same (and much farther away than Tulsa, OK.....)
We found that if we add the town to the first of the search, the results are much better:
(similar results are true for every search we did - various cities around the US and with other 'common places' like Quiznos (similar results as Walmart when in their home town of Denver, CO...)
As you can see from my code, I tried using the allowedRegion?.northEast and southWest (as I understand it, those should set the search area to about 100 miles around the location, though I'm not sure I set that up right), though no difference was found from this setup (i.e., with/without I get the same results).
The only 'better' results were by putting in the town name along with the 'common' one (though, oddly, different results were returned if the town name was before or after the common one - I didn't check exactly, though I think they are 'best' (i.e., locations are all pretty near) from putting the town name after the common one)
What can I do to get better results without having to tell the user to enter the town name (not a very desirable plan! :)
Thank you in advance for tips - the lookup is a key part of the app (not the test stuff shown in the pictures! ) and users expect to pull up several 'nearby' common places (in this case, we would expect to see all 5 results within something like 20 miles - certainly no more than 100 miles away), so it is an important thing for us that this work much more reliably than we are seeing now.

How would I use the same viewcontroller, but pass different information through it?

so, my current problem is as follows.
I would like to essentially use my current viewcontroller, but change the location settings somewhere else within the app and have the data pass though the viewcontroller with the updated location information.
I'll elaborate. I'm currently utilizing the following code for reverse geocoding, and obtaining the users location.
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
locationManager.stopUpdatingLocation()
if(locations.count > 0){
let location = locations[0] as! CLLocation
// println(location.coordinate)
if let currentLocatino = currLocation {
if CLLocation(latitude: currentLocatino.latitude, longitude: currentLocatino.longitude).distanceFromLocation(location) > 500 {
currLocation = location.coordinate
self.skip = 0
self.loadObjects()
}
}
else {
currLocation = location.coordinate
self.skip = 0
self.loadObjects()
}
CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: currLocation!.latitude, longitude: currLocation!.longitude), completionHandler: {(placemarks, error) -> Void in
if error != nil {
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks.count > 0 {
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateStyle = .MediumStyle
formatter.stringFromDate(date)
let pm = placemarks[0] as! CLPlacemark
var testifempty = "\(pm.subLocality)"
if testifempty == "nil"
{
self.locationManager.startUpdatingLocation()
if let lbutton = self.lbutton{
lbutton.text = "Hey " + "\(pm.locality)" //+ "\n" + formatter.stringFromDate(date)
}
}
else
{
self.locationManager.startUpdatingLocation()
if let lbutton = self.lbutton {
lbutton.text = "Hey " + "\(pm.subLocality)\n" // + formatter.stringFromDate(date)
}
}
}
else {
println("Problem with the data received from geocoder")
}
})
} else {
println("Cannot fetch your location")
}
}
What I would like, it to essentially pass through a NEW location and set a boolean of sorts so that if said boolean is set to false, the NEW location information is passed through the app.
I'm thinking about using a switch on the main viewcontroller, and changing the location on a secondary viewcontroller.
I'm sorry if this is a long post, but I feel as though I'm right on the edge of figuring out how to do this but I need to be sure I'm going in the right direction.
Maybe I could use a mapkit and use annotations?
I would not recommend using a view of any type to pass or hold information. A switch (like a UISwitch) should represent some other data in a graphical way for the user.
If you're new to mobile development, I'd recommend taking a look at the MVC design pattern. Here, are a couple different resources to get you started on that.
If you need to keep some data somewhere in your app that you can access from any class (viewController, view, custom object, etc.) then a Singleton would be a great solution. You can have map data that comes in be accessible across the entire app, you can store variables and be able to re-assign them from other classes, and you always end up with only one instance so your logical wires never get crossed.
Singletons get use fairly often, so I'd recommend looking at what the implementation code looks like and getting familiar with it. Here's a brief example of an implementation.
For your problem, the delegation pattern seems like the most appropriate choice.
If you create a protocol for your primary view controller that declares a method to update its data, such as locationDidUpdate:, then the primary view controller can be set as a delegate of the secondary view controller. The secondary view controller can then call this delegate method and update the primary view controller with new data.
As an example, a protocol can be declared in your first view controller like:
protocol LocationViewControllerDelegate: class {
func locationDidUpdate(location: CLLocation)
}
Then have your secondary view controller conform to the protocol and create a delegate property:
weak var delegate: LocationViewControllerDelegate?
When you need to update the location, call the delegate method with:
delegate.locationDidUpdate(myNewLocation)
Having a protocol for managing event-based updates is an advantage, in general, because it provides a clear contract that can easily be implemented by other classes.

Any api in iOS6 to get static map image of a gps location

I want a static map image from a location. I have the latitude and longitude of that location. Is there any api in iOS 6 to get the apple map static image of a gps location.
MKMapView has two properties to achieve this functionality: scrollEnabled and zoomEnabled. Set both to NO and you'll have a non-scrolling and non-zooming (thus static) map image.
I believe Google Static Maps is what you're looking for. Send it the parameters of location and it will return the image you want.
On iOS 7+ Apple MapKit allows displaying static images of a map using MKMapSnapshotter class. It is detailed in Creating a Snapshot of a Map section of MapKit documentation:
// Takes a snapshot and calls back with the generated UIImage
static func takeSnapshot(mapView: MKMapView, withCallback: (UIImage?, NSError?) -> ()) {
let options = MKMapSnapshotOptions()
options.region = mapView.region
options.size = mapView.frame.size
options.scale = UIScreen.mainScreen().scale
let snapshotter = MKMapSnapshotter(options: options)
snapshotter.startWithCompletionHandler() { snapshot, error in
guard snapshot != nil else {
withCallback(nil, error)
return
}
withCallback(snapshot!.image, nil)
}
}
just use MKMapView to display map ,you will have to use MapKit.framework for that
this link will help you...
https://github.com/kviksilver/MKMapview-annotation-grouping
Happy Coding!!!

Resources