Swift- MKMapkit view only one city?
I translated the Obj-C code found here: https://gist.github.com/Alp-Phone/e11cca67e77285566d4d to Swift Link is dead.
lazy var restrictedRegion: MKCoordinateRegion = {
// sets maps to univeristy
let location = CLLocationCoordinate2DMake(42.9633, -85.890042)
// Span and region
let span = MKCoordinateSpanMake (0.005, 0.005)
return MKCoordinateRegion(center: location, span: span)
}()
override func viewDidLoad() {
super.viewDidLoad()
mapView.setRegion(restrictedRegion, animated: true)
}
var manuallyChangingMap = false //Stop from updating while animating
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if !manuallyChangingMap && ((mapView.region.span.latitudeDelta > restrictedRegion.span.latitudeDelta * 4) ||
(mapView.region.span.longitudeDelta > restrictedRegion.span.longitudeDelta * 4) ||
fabs(mapView.region.center.latitude - restrictedRegion.center.latitude) > restrictedRegion.span.latitudeDelta ||
fabs(mapView.region.center.longitude - restrictedRegion.center.longitude) > restrictedRegion.span.longitudeDelta) {
manuallyChangingMap = true
mapView.setRegion(restrictedRegion, animated: true)
manuallyChangingMap = false
}
}
iOS - How to limit the MapView to a specific region?
By implementing :
-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
You should be able to reset region to your specific one.
Look at that answer to have an example of how to do this.
Another solution, that brings more precise events but is more complex (I'm currently using this successfully for another need) is to subclass MKMapView
and override UIScrollViewDelegate
protocol.
If you do that, make sure to call super
implementation like that :
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if ([super respondsToSelector:@selector(scrollViewWillBeginDragging:)])
[super scrollViewWillBeginDragging:scrollView];
// do what you want
}
Note: While the protocol is public, MKMapView
implementation is unknown and might differ with iOS versions, so it is more safe to check with respondsToSelector:
before. You must call super implementations or Map will just not work properly.
Set mapkit to location with only a cityname
You'd use forward geocoding with CLGeocoder using the geocodeAddressString method. More info at:
https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLGeocoder_class/index.html#//apple_ref/occ/instm/CLGeocoder/geocodeAddressString:completionHandler:
Or you could hit Google Maps services as well.
How to display a MKMapView without street names?
What you need to do, is to set the maptype to Satellite.
Here's how to do this:
[mapView setMapType:MKMapTypeSatellite];
EDIT:
Not sure if it's satellite, but try one of these:
[mapView setMapType:MKMapTypeStandard];
[mapView setMapType:MKMapTypeHybrid];
[mapView setMapType:MKMapTypeSatellite];
Related Topics
How to Disabled Some Default Functionality in Scene View When Allowscameracontrol = True
Disable Bringing App Window to Front. After Closing Another Window
Need Clarification on Anyobject in Swift
How to Find The Top 3 Maximum Values in a Swift Dictionary
Exc_Badinstruction When Computing a Hash Value
Swiftui Conditional View Transitions Are Not Working
Using a Swiftui List Sidebar in a UIsplitviewcontroller
Allocating Ekeventstore Throws Warnings
Generic Vector with Cardinality Type Safety
Swift 4 Base64 String to Data Not Working Due to String Containing "Incomplete" Emoji
Swift Nstimer Dynamically Changing Interval
Swift Displaying The Time or Date Based on Timestamp
Is It a Good Way to Access Instance Variable with Self? If I Use a Lot
Struct Hash for Different Types
Swiftui Index Out of Range in Foreach
Gmsmarker Not Appearing on Map