Google Maps Bounds inaccurate - javascript

my problem is related to the way how LatLngBounds.extend() method works. I am not sure if this is a bug or not but it does not work as intended. I expect bounds to contain all points included in it. Unfortunately it works really strangely in my case. Please check this fiddle: http://jsfiddle.net/Sy5dC/4/.
var locationBBOX = [
'(42.00, -142.00) (70.00, -50.00)', //CANADA
'(47.274719, 5.864166000000068) (55.053055, 15.038886999999932)',//GERMANY
//'(40.00, 27.00) (70.00, 179.00)'//RUSSIA
];
In lines 23-27 you will find definitions of bounding boxes for 3 countries. Currently Russia is commented out and everything works like a charm. The problem begins when Russia is included in bounds - the map does not show the correct bounding box. Is there anything I can do to make it work?
Thanks!

boundingBoxPoints does not encompass the same area as bbox does.
You may create the correct box for the bounds by using a Rectangle with the bounds-property set to bbox, and when you create Markers for all the points you will see that they are all located within this box.
Demo(the blue rectangle marks the real bounds): http://fiddle.jshell.net/doktormolle/EGDFH/show/
But I'm afraid that's not the desired result, unfortunately I have no solution at this time, also union() will not help here(it seems to be just a wrapper for extend(sw) & extend(ne) ).

Related

Why are the text labels in d3 transforming to zero on pan/zoom?

I'm fairly new at d3 and I've built the code from a couple of sources so it's probably just that I have it initializing incorrectly but I'm not seeing what it is. The page I'm building dynamically fetches geojson from a database and renders a simple map with a label. This works correctly but whe I added code to give the map zoom/pan functionality the map behaves correctly but the transform on the text moves the label to 0 postition with the <g> element and I'm not sure why? When I zoom in the map and text increases in size as expected but the text position stays at 0/0.
Instead of dumping a lot of code here I've put together this fiddle so that it could easily be seen and tested.
I think that anyone more familiar with d3 probably knows what I've got wrong here. I would sure appreciate someone pointing it out to me. Thanks
See your problem saved in the fiddle:
const items = g.selectAll('g.item')
.data(bb.features)
.enter()
.append('g')
.classed('item', true);
items.append('path').attr(...)
items.append('text').attr(...)
Instead of entering path and text elements separately, enter a g container and then append path and text under the g.

Masked element in Snap SVG doesn't come into view when translated

I have a group of elements that are masked by a rect in SnapSVG and I want to translate the elements, bringing new ones into view (and hiding ones that are currently in view). The code is really simple - here's a codepen: http://codepen.io/austinclemens/pen/ZbpVmX
As you can see from the pen, box1, which starts outside the mask element (clip) should cross through it when animated, but it never appears. Moreover, box2, which should move out of the clipping area, remains visible.
This example seems to do a similar thing and has no problems: http://svg.dabbles.info/snaptut-masks2
Here's the code from codepen:
var t = Snap('#target')
var clip=t.rect(200,200,200,200).attr({fill:'#fff'})
var box1=t.rect(300,100,50,50).attr({fill:'#000'})
var box2=t.rect(300,300,50,50).attr({fill:'#000'})
var boxgroup=t.group(box1,box2)
boxgroup.attr({mask:clip})
boxgroup.animate({transform:'t100,300'},2000)
I notice that the svg.dabbles examples translates the clip region by 0,0 at one point, but adding something like that doesn't seem to get me anywhere.
Ok, I figured this out thanks in part to this really great article about SVG transforms: http://sarasoueidan.com/blog/svg-transformations/
The upshot is that when I translate the box group, it takes the mask with it. This is a little confusing to me still - I guess the mask attribute is causing this somehow? Anyways, the solution is to apply an opposite translation to the mask to keep it in place. Check the pen to see it in action but basically I just had to add:
clip.animate({transform:'t-100,-300'},2000)
The tricky part of this is that you now need to synchronize the movement of the mask and the movement of the box group.
edit - I now demonstrate how synchronization can be achieved using snap's set.animate method on the codepen.

moving path to front changes selection?

i'm struggling to understand the following behaviour: i have two maps (based on topojson-data, visualised through d3), and on mouseover over certain parts of map1, the corresponding parts of map2 should light up. i got it to work with changing the style (opacity or fill), but now i wanted to highlight the borders of each map-part.
as seen for instance here one needs to move the specific path to the front to make all the borders visible. this is no problem for the area where i move the mouse across (using this), but when i select the corresponding part of the other map, it works one time and after that other parts get selected - so my guess is something is messing with the selection.
here is the code:
.on("mouseover",function(d){
var old=d.properties.iso; //this is the identifying number of the map-part(s)
sel=svg2.selectAll("path")
.data(datastore2015.features)
.filter(function(d){return d.properties.iso==old;})
.node(); //here the corresponding part(s) get filtered
d3.select(sel.parentNode.appendChild(sel)).classed("high2",true); //and this moves it to front and highlights the borders
on mouseout, it just resets:
.on("mouseout",function(d){
svg2.selectAll("path").classed("high2",false);
when i log the data to the console it seems that each mouseover moves +1 entry through the dataset, starting by the first entry the mouse moved over. i could not figure out why this happens and how to avoid it.
i'd appreciate any ideas you could give me, mainly i'd like to understand what's going wrong and why.
thanks
so i found my error, calling the data-variable once again seems to have messed things up - somehow i was under the impression that i need it, but it works just fine this way:
sel=svg2.selectAll("path").filter(function(d){return d.properties.iso==old;}).node();
d3.select(sel.parentNode.appendChild(sel)).classed("high2",true);
sorry for the bother, i didn't see this possibility before.

Which 3 marker creates a triangle that contains specified point?

I have a map with ~50 markers on it (Google maps api v3) and I'd like to click a random point and get which 3 markers surrounds it.
I've found this example, but it doesn't really do the thing i want alone.
Here's an image of my markers and what I'm trying to accomplish:
[IMAGE]
According to image;
When I click to red point, the script should return me the coordinates of those 3 markers connected with yellow lines.
Same for blue point and green lines.
It has to be the narrowest, smallest triangle.
I've tried finding closest 3 markers, it works for most situations but doesn't cover all. (Like when there's 3 markers in the same line with a close range.)
Any advice? Thanks in advance!
Edit
ps. I can use PHP with ajax calls if it helps.
Did it using PHP, by looping all marker coordinates.
First I checked if a point is in the triangle using this class
Then checked if its' area is smaller than the last one.
/* 3 foreach loops{ */
$pointLocation = new pointLocation();
$polygon = array($A['y'].' '.$A['x'], $B['y'].' '.$B['x'], $C['y'].' '.$C['x'], $A['y'].' '.$A['x']);
$point = $P['y'].' '.$P['x'];
if(($pointLocation->pointInPolygon($point, $polygon)=='inside' || $pointLocation->pointInPolygon($point, $polygon) == 'vertex'))
// Here I check the area using another function and save this coordinates if this is the smallest triangle. Then it continues to loop
/* } */
This is probably not the most efficient way, but unless I find another way, this'll do the job.

How to make area of image clickable

This is not a: "Do all the work for me!" kind of question. I just wanna know which approach you think would be suitable for this challenge.
I have this map:
As you can see by the blue marker, I've roughly drawned some selections/areas of the map. Theese areas I want to serve as links.
But I don't quite know how to grasp this challenge, since all of the areas have quite odd shapes.
I have looked at cords, but it seems like a huge job with all of the twists and turns that I would need to do.
I would be awesome if I could just slice up the areas in Photoshop and save each of them as .png and just tell my page to ignore the transparent area! But that's just wishfull thinking I suppose.
I hope that one of you have a suggestion that I've overlooked.
Give a try to these -
http://polymaps.org/
http://www.amcharts.com/javascript-maps/
Raphael JS
You can try making an SVG version of your map and then implement it's clickiness with one of these libraries depending on which one you choose.
Here's one tutorial to do this with Raphael JS - http://parall.ax/blog/view/2985/tutorial-creating-an-interactive-svg-map
Make an image for each clickeable zone, like this:
Register to the click event of the img element from the page, this way:
var getAreaFromXY = function(x,y) {
// for each section colored map
// get pixel color on x,y (see http://stackoverflow.com/questions/8751020/how-to-get-a-pixels-x-y-coordinate-color-from-an-image)
// if the color is red, that is the zone
};
$(".post-text img").click(function(e) {
var area = getAreaFromXY(e.offsetX, e.offsetY);
});

Categories

Resources