Define center of map in Mapbox - mapbox-gl-js

I'm creating a viewer using Mapbox, the viewer will be full screen. However one half of the screen will be covered with a floating UI component. Therefor if the map zooms to a feature it should position the feature in the center of the other half of the screen. What would be a good way to approach this?
Currently when I zoom to a feature it will look like this, covered by the UI component:
I want it to automatically center on one half of the viewer, like this:

The fitBounds method allows you to pass an option for padding, for example:
map.fitBounds([[min_lon, min_lat], [max_lon, max_lat]], {
padding: {
top: 5,
bottom: 5,
left: 5,
right: document.getElementById('your_div').offsetWidth + 5
},
linear: true,
duration: 0
});

Related

How to avoid polygon distortion when zooming?

Here is a full jsfiddle example
I use a custom series and draw a polygon:
data = [
[80.9251933067, 207.9047427038],
[52.8853803102, 337.7443022089],
[25.9926385814, 120.3586150136]
];
I use echarts.graphi.clipPointsByRect() (like in this echarts-example) to make sure, the polygon is not drawn outside of the grid.
echarts.graphic.clipPointsByRect(points, {
x: params.coordSys.x,
y: params.coordSys.y,
width: params.coordSys.width,
height: params.coordSys.height
})
Initially the polygon is drawn correctly, like this:
But when I zoom in, the polygon is distorted: e.g. you can click the zoom part buttom below the chart to zoom from 40 to 60 - in this case I'd expect to see the part of the shape (like highlighted in yellow in the image above) - but instead I see this distorted image:
Maybe this function is not meant for this use-case, or is this a bug?
Is there another function for this use-case or does anyone know a workaround?
Update
Version 4.4.x contains a new clip feature. This makes it easy to avoid the distortion:
in the render function we don't need to clip our shapes: i.e. no need to call clipPointsByRect()
instead we just activate clip on the custom series:
New series definition with clip: 'true':
series: [{
type: 'custom',
clip: 'true',
renderItem: renderItem,
data: data
}]
Here is an updated jsfiddle expample
Original Version
it seems that the function is really not working as expected - see echarts-source code comment:
export function clipPointsByRect(points, rect) {
// FIXME: this way migth be incorrect when grpahic clipped by a corner.
// and when element have border.
I've created an issue #10222 for the e-charts project
A workaround for now is to use a custom clipping function
e.g. lineclip supports the Sutherland-Hodgman algorithm for polygon clipping
Here is the updated jsfiddle-example that shows the correct result when you zoom in:

Prevent Mobile Safari from scrolling address bar into view when clicking on top of the page

I have a webapp that on load performs window.scrollTo(0, 1); to hide the address bar which is working.
One of the elements is a header with fixed positioning of top: 0, causing it to stay topmost of the viewport. On this header there are a few clickable buttons, when you try to click them, instead of performing the action it scrolls the address bar into view.
This is a default safari behavior for the click on the top 15-20 pixels of the screen.
I have tried to capture the clicks and cancel the event, cancel bubbling, prevent default etc. None of which seemed to work.
The code I tried was adding a div with id test:
#test {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 20px;
}
document.getElementById("test").addEventListener("click", function (event) {
event.preventDefault();
event.stopPropagation();
}, false);
Any ideas?
I use this for preventing default on mobile:
https://github.com/alexblack/google-fastbutton
$('#your-button').fastClick(function(e) {
e.preventDefault();
});
No address bar is shown. Pretty neat for UX also

Make Firefox Panel fit content

I'm manually porting an extension I wrote in Chrome over to Firefox. I'm attaching a panel to a widget, and setting the content of that panel as an HTML file. How can I make the panel shrink and grow with the content? There's a lot of unsightly scroll bars and grey background right now.
var data = require("self").data;
var text_entry = require("panel").Panel({
width: 320,
height: 181,
contentURL: data.url("text-entry.html"),
contentScriptFile: data.url("get-text.js")
});
require("widget").Widget({
label: "Text entry",
id: "text-entry",
contentURL: "http://www.mozilla.org/favicon.ico",
panel: text_entry
});
Not setting the height property of the panel makes it quite tall.
You might want to check out this example that resizes the panel based on the document loaded. If you want to resize based on changes to the content size, at least on initial load:
https://builder.addons.mozilla.org/package/150225/latest/
( sorry for the delay in respinding, been afk travelling )

browser size html css

I have been using the following css code:
#MainBox
{ width: 488px;
height: 181px;
position: absolute;
left: 50%;
top: 236px;
margin-left: -244px; //this being half the width
}
To ensure that the items on the page are centred. The problem is, when viewing this on an iphone (and i'm assuming similar on other smartphones) the left hand side of the page is chopped off! Does anyone know how I can resolve this issue and bring everything into fit?
Thank you!
You're moving the element to the left by 244px with that CSS, and the iphone screen being so small this is causing it to be cut off. Try this:
#MainBox{
width: 488px;
height: 181px;
margin: 236px auto 0 auto;
}
The technique above to center a div is a little obsolete.
Use margin: auto, width to a fixed value (which you already have), and make the parent position:relative.

How to modify the position of an Image object?

In GWT, how do you set the position of an Image object after it was added to a Panel? There does not seem to be a method to set the Image top and left positions. Do we have to remove the image from the Panel and add it again using the desired left and top values? What is the best approach?
If you set absolute positions, you can
int top = myPanel.getAbsoluteTop();
int left = myPanel.getAbsoluteLeft();
myImage.getElement().getStyle().setTop(top + 100, Unit.PX);
myImage.getElement().getStyle().setLeft(left + 100, Unit.PX);
Alternatively, you can set "position: relative" style on your image. Then you can set "top" and "left" on an image, and it would position itself relative to its parent element.
From the way your questions is asked, I assume that you are trying to use GWT as you would use Swing or SWT from Java. This is not the way GWT is ment to used and leads to very bad code and very bad projects most of the time.
GWT is not about hiding the browser. So if you want to change the postion of an element you would do that mostly with CSS.
Simply add two css classes to your project
.before{
position: relative;
top: 0px;
left: 0px;
}
.after{
position: relative;
top: 100px;
left: 100px;
}
at first give your image the first class:
image.addStyleName("before");
and later:
image.addStyleName("after");
If you really need to make this dynamically (some calculated size) you can just set the style property of the element:
image.getElement().getStyle().setLeft(value, Unit.PX);
image.getElement().getStyle().setTop(value, Unit.PX);
What I did was simply:
absolutePanel.setWidgetPosition(myImage, newXvalue, newYvalue);
Assuming that 'myImage' was a child of absolutePanel.