How do I intercept mapview panning and zooming (Swift 4)
I'm trying to restrict panning and zooming for a mapview, to remain within a predefined max and min camera altitude and the four corners of a bounding box.
The solutions proposed here, allow the user to pan outside the region, and then use regionDidChangeAnimated
to pan them back to within the pre-defined bounds.
I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place. Can I, for example, intercept panning/zooming gestures and decide whether or not to let them continue?
I think I'm looking for something like the following pseudocode:
func blockMapViewPanEvent(_ mapView: MKMapView){
return mapView.visibleMapRect.isContainedWithin(topLeft, bottomRight)
}
I know that MapBox's Swift SDK achieves this, so I imagine it's possible. Can I do this with plain-old MapKit?
Edit for Clarity:
I want to restrict panning and zooming so the user can pan and zoom as much as they want within a bounding box. For example, if the bounding box is of Germany, I want the user to be able to pan and zoom within Germany, but they should never be able to see Spain, since Spain is outside the bounding box of Germany.
I do not want to disable all user interaction on the map view.
ios swift mapkit mkmapview uigesturerecognizer
|
show 1 more comment
I'm trying to restrict panning and zooming for a mapview, to remain within a predefined max and min camera altitude and the four corners of a bounding box.
The solutions proposed here, allow the user to pan outside the region, and then use regionDidChangeAnimated
to pan them back to within the pre-defined bounds.
I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place. Can I, for example, intercept panning/zooming gestures and decide whether or not to let them continue?
I think I'm looking for something like the following pseudocode:
func blockMapViewPanEvent(_ mapView: MKMapView){
return mapView.visibleMapRect.isContainedWithin(topLeft, bottomRight)
}
I know that MapBox's Swift SDK achieves this, so I imagine it's possible. Can I do this with plain-old MapKit?
Edit for Clarity:
I want to restrict panning and zooming so the user can pan and zoom as much as they want within a bounding box. For example, if the bounding box is of Germany, I want the user to be able to pan and zoom within Germany, but they should never be able to see Spain, since Spain is outside the bounding box of Germany.
I do not want to disable all user interaction on the map view.
ios swift mapkit mkmapview uigesturerecognizer
@DaniSpringer I'm not programmatically updating the map location at all. I think I'm trying to figure out how to do exactly what you're describing: intercept the pan, check the new position, and ignore it if it does not meet some criteria. Thanks for you help!
– RP-3
Nov 23 '18 at 6:12
Possibly related: stackoverflow.com/questions/13539521/…
– Daniel Springer
Nov 23 '18 at 6:19
@DaniSpringer I've referenced that first link in the question and stated how this question differs to that, and the answers provided there. Specificaly: "The solutions proposed here, allow the user to pan outside the region, and then useregionDidChangeAnimated
to pan them back to within the pre-defined bounds. I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place."
– RP-3
Nov 25 '18 at 10:54
Perhaps you can adapt that code, by changing the lines that would "check if out of region, and if yes, move back", to "check if the new location of pan is outside of region, and if yes, ignore pan gesture". The way pan gesture works is the phone detects a movement (gesture) and provides its data to the app. So the app can handle that so that if the data provided show that the new location to update the element to (in this case, a map) is beyond a certain limit (more or less than something), you can do something about it, for example: ignore it entirely, and not update the element's position.
– Daniel Springer
Nov 25 '18 at 19:55
I use this for draggable textfields:@objc func myFunc(gesture: UIPanGestureRecognizer) { self.view.bringSubviewToFront(myTextField) let translation = gesture.translation(in: self.view) myTextFieldXConstraint.constant = myTextFieldXConstraint.constant + translation.x myTextFieldYConstraint.constant = myTextFieldYConstraint.constant + translation.y myTextField.center = CGPoint(x: myTextField.center.x + translation.x, y: myTextField.center.y + translation.y) gesture.setTranslation(CGPoint.zero, in: self.view) }
– Daniel Springer
Nov 25 '18 at 19:58
|
show 1 more comment
I'm trying to restrict panning and zooming for a mapview, to remain within a predefined max and min camera altitude and the four corners of a bounding box.
The solutions proposed here, allow the user to pan outside the region, and then use regionDidChangeAnimated
to pan them back to within the pre-defined bounds.
I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place. Can I, for example, intercept panning/zooming gestures and decide whether or not to let them continue?
I think I'm looking for something like the following pseudocode:
func blockMapViewPanEvent(_ mapView: MKMapView){
return mapView.visibleMapRect.isContainedWithin(topLeft, bottomRight)
}
I know that MapBox's Swift SDK achieves this, so I imagine it's possible. Can I do this with plain-old MapKit?
Edit for Clarity:
I want to restrict panning and zooming so the user can pan and zoom as much as they want within a bounding box. For example, if the bounding box is of Germany, I want the user to be able to pan and zoom within Germany, but they should never be able to see Spain, since Spain is outside the bounding box of Germany.
I do not want to disable all user interaction on the map view.
ios swift mapkit mkmapview uigesturerecognizer
I'm trying to restrict panning and zooming for a mapview, to remain within a predefined max and min camera altitude and the four corners of a bounding box.
The solutions proposed here, allow the user to pan outside the region, and then use regionDidChangeAnimated
to pan them back to within the pre-defined bounds.
I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place. Can I, for example, intercept panning/zooming gestures and decide whether or not to let them continue?
I think I'm looking for something like the following pseudocode:
func blockMapViewPanEvent(_ mapView: MKMapView){
return mapView.visibleMapRect.isContainedWithin(topLeft, bottomRight)
}
I know that MapBox's Swift SDK achieves this, so I imagine it's possible. Can I do this with plain-old MapKit?
Edit for Clarity:
I want to restrict panning and zooming so the user can pan and zoom as much as they want within a bounding box. For example, if the bounding box is of Germany, I want the user to be able to pan and zoom within Germany, but they should never be able to see Spain, since Spain is outside the bounding box of Germany.
I do not want to disable all user interaction on the map view.
ios swift mapkit mkmapview uigesturerecognizer
ios swift mapkit mkmapview uigesturerecognizer
edited Nov 23 '18 at 5:50
RP-3
asked Nov 23 '18 at 5:14
RP-3RP-3
10511
10511
@DaniSpringer I'm not programmatically updating the map location at all. I think I'm trying to figure out how to do exactly what you're describing: intercept the pan, check the new position, and ignore it if it does not meet some criteria. Thanks for you help!
– RP-3
Nov 23 '18 at 6:12
Possibly related: stackoverflow.com/questions/13539521/…
– Daniel Springer
Nov 23 '18 at 6:19
@DaniSpringer I've referenced that first link in the question and stated how this question differs to that, and the answers provided there. Specificaly: "The solutions proposed here, allow the user to pan outside the region, and then useregionDidChangeAnimated
to pan them back to within the pre-defined bounds. I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place."
– RP-3
Nov 25 '18 at 10:54
Perhaps you can adapt that code, by changing the lines that would "check if out of region, and if yes, move back", to "check if the new location of pan is outside of region, and if yes, ignore pan gesture". The way pan gesture works is the phone detects a movement (gesture) and provides its data to the app. So the app can handle that so that if the data provided show that the new location to update the element to (in this case, a map) is beyond a certain limit (more or less than something), you can do something about it, for example: ignore it entirely, and not update the element's position.
– Daniel Springer
Nov 25 '18 at 19:55
I use this for draggable textfields:@objc func myFunc(gesture: UIPanGestureRecognizer) { self.view.bringSubviewToFront(myTextField) let translation = gesture.translation(in: self.view) myTextFieldXConstraint.constant = myTextFieldXConstraint.constant + translation.x myTextFieldYConstraint.constant = myTextFieldYConstraint.constant + translation.y myTextField.center = CGPoint(x: myTextField.center.x + translation.x, y: myTextField.center.y + translation.y) gesture.setTranslation(CGPoint.zero, in: self.view) }
– Daniel Springer
Nov 25 '18 at 19:58
|
show 1 more comment
@DaniSpringer I'm not programmatically updating the map location at all. I think I'm trying to figure out how to do exactly what you're describing: intercept the pan, check the new position, and ignore it if it does not meet some criteria. Thanks for you help!
– RP-3
Nov 23 '18 at 6:12
Possibly related: stackoverflow.com/questions/13539521/…
– Daniel Springer
Nov 23 '18 at 6:19
@DaniSpringer I've referenced that first link in the question and stated how this question differs to that, and the answers provided there. Specificaly: "The solutions proposed here, allow the user to pan outside the region, and then useregionDidChangeAnimated
to pan them back to within the pre-defined bounds. I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place."
– RP-3
Nov 25 '18 at 10:54
Perhaps you can adapt that code, by changing the lines that would "check if out of region, and if yes, move back", to "check if the new location of pan is outside of region, and if yes, ignore pan gesture". The way pan gesture works is the phone detects a movement (gesture) and provides its data to the app. So the app can handle that so that if the data provided show that the new location to update the element to (in this case, a map) is beyond a certain limit (more or less than something), you can do something about it, for example: ignore it entirely, and not update the element's position.
– Daniel Springer
Nov 25 '18 at 19:55
I use this for draggable textfields:@objc func myFunc(gesture: UIPanGestureRecognizer) { self.view.bringSubviewToFront(myTextField) let translation = gesture.translation(in: self.view) myTextFieldXConstraint.constant = myTextFieldXConstraint.constant + translation.x myTextFieldYConstraint.constant = myTextFieldYConstraint.constant + translation.y myTextField.center = CGPoint(x: myTextField.center.x + translation.x, y: myTextField.center.y + translation.y) gesture.setTranslation(CGPoint.zero, in: self.view) }
– Daniel Springer
Nov 25 '18 at 19:58
@DaniSpringer I'm not programmatically updating the map location at all. I think I'm trying to figure out how to do exactly what you're describing: intercept the pan, check the new position, and ignore it if it does not meet some criteria. Thanks for you help!
– RP-3
Nov 23 '18 at 6:12
@DaniSpringer I'm not programmatically updating the map location at all. I think I'm trying to figure out how to do exactly what you're describing: intercept the pan, check the new position, and ignore it if it does not meet some criteria. Thanks for you help!
– RP-3
Nov 23 '18 at 6:12
Possibly related: stackoverflow.com/questions/13539521/…
– Daniel Springer
Nov 23 '18 at 6:19
Possibly related: stackoverflow.com/questions/13539521/…
– Daniel Springer
Nov 23 '18 at 6:19
@DaniSpringer I've referenced that first link in the question and stated how this question differs to that, and the answers provided there. Specificaly: "The solutions proposed here, allow the user to pan outside the region, and then use
regionDidChangeAnimated
to pan them back to within the pre-defined bounds. I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place."– RP-3
Nov 25 '18 at 10:54
@DaniSpringer I've referenced that first link in the question and stated how this question differs to that, and the answers provided there. Specificaly: "The solutions proposed here, allow the user to pan outside the region, and then use
regionDidChangeAnimated
to pan them back to within the pre-defined bounds. I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place."– RP-3
Nov 25 '18 at 10:54
Perhaps you can adapt that code, by changing the lines that would "check if out of region, and if yes, move back", to "check if the new location of pan is outside of region, and if yes, ignore pan gesture". The way pan gesture works is the phone detects a movement (gesture) and provides its data to the app. So the app can handle that so that if the data provided show that the new location to update the element to (in this case, a map) is beyond a certain limit (more or less than something), you can do something about it, for example: ignore it entirely, and not update the element's position.
– Daniel Springer
Nov 25 '18 at 19:55
Perhaps you can adapt that code, by changing the lines that would "check if out of region, and if yes, move back", to "check if the new location of pan is outside of region, and if yes, ignore pan gesture". The way pan gesture works is the phone detects a movement (gesture) and provides its data to the app. So the app can handle that so that if the data provided show that the new location to update the element to (in this case, a map) is beyond a certain limit (more or less than something), you can do something about it, for example: ignore it entirely, and not update the element's position.
– Daniel Springer
Nov 25 '18 at 19:55
I use this for draggable textfields:
@objc func myFunc(gesture: UIPanGestureRecognizer) { self.view.bringSubviewToFront(myTextField) let translation = gesture.translation(in: self.view) myTextFieldXConstraint.constant = myTextFieldXConstraint.constant + translation.x myTextFieldYConstraint.constant = myTextFieldYConstraint.constant + translation.y myTextField.center = CGPoint(x: myTextField.center.x + translation.x, y: myTextField.center.y + translation.y) gesture.setTranslation(CGPoint.zero, in: self.view) }
– Daniel Springer
Nov 25 '18 at 19:58
I use this for draggable textfields:
@objc func myFunc(gesture: UIPanGestureRecognizer) { self.view.bringSubviewToFront(myTextField) let translation = gesture.translation(in: self.view) myTextFieldXConstraint.constant = myTextFieldXConstraint.constant + translation.x myTextFieldYConstraint.constant = myTextFieldYConstraint.constant + translation.y myTextField.center = CGPoint(x: myTextField.center.x + translation.x, y: myTextField.center.y + translation.y) gesture.setTranslation(CGPoint.zero, in: self.view) }
– Daniel Springer
Nov 25 '18 at 19:58
|
show 1 more comment
1 Answer
1
active
oldest
votes
If I am understanding you correctly, you want to disable user interaction in the map.
Here are the few options available in map view. These might be helpful for you
mapView.zoomEnabled = false
mapView.scrollEnabled = false
mapView.userInteractionEnabled = false
If you have issues with these you can simply put a transparent view over map view
That's not quite what I'm after. I want the user to be able to pan and zoom within a certain area. They should not be able to pan/zoom outside that area. I've edited the question for clarity.
– RP-3
Nov 23 '18 at 5:51
I think the question asks how to allow panning and zooming but only within a desired area.
– Magnas
Nov 23 '18 at 5:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440957%2fhow-do-i-intercept-mapview-panning-and-zooming-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I am understanding you correctly, you want to disable user interaction in the map.
Here are the few options available in map view. These might be helpful for you
mapView.zoomEnabled = false
mapView.scrollEnabled = false
mapView.userInteractionEnabled = false
If you have issues with these you can simply put a transparent view over map view
That's not quite what I'm after. I want the user to be able to pan and zoom within a certain area. They should not be able to pan/zoom outside that area. I've edited the question for clarity.
– RP-3
Nov 23 '18 at 5:51
I think the question asks how to allow panning and zooming but only within a desired area.
– Magnas
Nov 23 '18 at 5:52
add a comment |
If I am understanding you correctly, you want to disable user interaction in the map.
Here are the few options available in map view. These might be helpful for you
mapView.zoomEnabled = false
mapView.scrollEnabled = false
mapView.userInteractionEnabled = false
If you have issues with these you can simply put a transparent view over map view
That's not quite what I'm after. I want the user to be able to pan and zoom within a certain area. They should not be able to pan/zoom outside that area. I've edited the question for clarity.
– RP-3
Nov 23 '18 at 5:51
I think the question asks how to allow panning and zooming but only within a desired area.
– Magnas
Nov 23 '18 at 5:52
add a comment |
If I am understanding you correctly, you want to disable user interaction in the map.
Here are the few options available in map view. These might be helpful for you
mapView.zoomEnabled = false
mapView.scrollEnabled = false
mapView.userInteractionEnabled = false
If you have issues with these you can simply put a transparent view over map view
If I am understanding you correctly, you want to disable user interaction in the map.
Here are the few options available in map view. These might be helpful for you
mapView.zoomEnabled = false
mapView.scrollEnabled = false
mapView.userInteractionEnabled = false
If you have issues with these you can simply put a transparent view over map view
answered Nov 23 '18 at 5:39
SatishSatish
1,036615
1,036615
That's not quite what I'm after. I want the user to be able to pan and zoom within a certain area. They should not be able to pan/zoom outside that area. I've edited the question for clarity.
– RP-3
Nov 23 '18 at 5:51
I think the question asks how to allow panning and zooming but only within a desired area.
– Magnas
Nov 23 '18 at 5:52
add a comment |
That's not quite what I'm after. I want the user to be able to pan and zoom within a certain area. They should not be able to pan/zoom outside that area. I've edited the question for clarity.
– RP-3
Nov 23 '18 at 5:51
I think the question asks how to allow panning and zooming but only within a desired area.
– Magnas
Nov 23 '18 at 5:52
That's not quite what I'm after. I want the user to be able to pan and zoom within a certain area. They should not be able to pan/zoom outside that area. I've edited the question for clarity.
– RP-3
Nov 23 '18 at 5:51
That's not quite what I'm after. I want the user to be able to pan and zoom within a certain area. They should not be able to pan/zoom outside that area. I've edited the question for clarity.
– RP-3
Nov 23 '18 at 5:51
I think the question asks how to allow panning and zooming but only within a desired area.
– Magnas
Nov 23 '18 at 5:52
I think the question asks how to allow panning and zooming but only within a desired area.
– Magnas
Nov 23 '18 at 5:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440957%2fhow-do-i-intercept-mapview-panning-and-zooming-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@DaniSpringer I'm not programmatically updating the map location at all. I think I'm trying to figure out how to do exactly what you're describing: intercept the pan, check the new position, and ignore it if it does not meet some criteria. Thanks for you help!
– RP-3
Nov 23 '18 at 6:12
Possibly related: stackoverflow.com/questions/13539521/…
– Daniel Springer
Nov 23 '18 at 6:19
@DaniSpringer I've referenced that first link in the question and stated how this question differs to that, and the answers provided there. Specificaly: "The solutions proposed here, allow the user to pan outside the region, and then use
regionDidChangeAnimated
to pan them back to within the pre-defined bounds. I'd like to know if it's possible to just never allow the user to pan outside the bounds in the first place."– RP-3
Nov 25 '18 at 10:54
Perhaps you can adapt that code, by changing the lines that would "check if out of region, and if yes, move back", to "check if the new location of pan is outside of region, and if yes, ignore pan gesture". The way pan gesture works is the phone detects a movement (gesture) and provides its data to the app. So the app can handle that so that if the data provided show that the new location to update the element to (in this case, a map) is beyond a certain limit (more or less than something), you can do something about it, for example: ignore it entirely, and not update the element's position.
– Daniel Springer
Nov 25 '18 at 19:55
I use this for draggable textfields:
@objc func myFunc(gesture: UIPanGestureRecognizer) { self.view.bringSubviewToFront(myTextField) let translation = gesture.translation(in: self.view) myTextFieldXConstraint.constant = myTextFieldXConstraint.constant + translation.x myTextFieldYConstraint.constant = myTextFieldYConstraint.constant + translation.y myTextField.center = CGPoint(x: myTextField.center.x + translation.x, y: myTextField.center.y + translation.y) gesture.setTranslation(CGPoint.zero, in: self.view) }
– Daniel Springer
Nov 25 '18 at 19:58