fitBounds(LatLngBounds) zooming fully out - google-maps-api-3

I'm setting a map to fitBounds using this line
var bounds = new google.maps.LatLngBounds(fromLoc, theLoc);
console.log(bound);
map.fitBounds(bounds);
As you can see when that line runs the map fully zooms out despite the 2 LatLng's only being around 20 minutes drive away.
Anyone know how I can fix this?

When you do this:
var bounds = new google.maps.LatLngBounds(fromLoc, theLoc);
You are creating a google.maps.LatLngBounds with the southWest corner at fromLoc and the northEast corner at theLoc.
To make a bounds that contains those two locations, do this instead:
var bounds = new google.maps.LatLngBounds(); // empty bounds
bounds.extend(fromLoc);
bounds.extend(theLoc);
map.fitBounds(bounds);

Related

How to determine if point is inside box (three.js)?

If we have NON-axis-aligned box, how can we best check if a point lies inside it? (I'm using three.js, so any utility from there can be of help. Three.js contains bounding box concept, but that is axis-aligned bounding box)
If your box is a THREE.BoxGeometry that is rotated, translated and scaled, then you can use its transformation matrix m to find if it intersects your point v:
transform v and the box by the inverse of m
check if transformed v is inside the transformed box (which is now axis aligned)
Here is the code:
var box = <Your non-aligned box>
var point = <Your point>
box.geometry.computeBoundingBox(); // This is only necessary if not allready computed
box.updateMatrixWorld(true); // This might be necessary if box is moved
var boxMatrixInverse = new THREE.Matrix4().getInverse(box.matrixWorld);
var inverseBox = box.clone();
var inversePoint = point.clone();
inverseBox.applyMatrix(boxMatrixInverse);
inversePoint.applyMatrix4(boxMatrixInverse);
var bb = new THREE.Box3().setFromObject(inverseBox);
var isInside = bb.containsPoint(inversePoint);
And here is a running demonstration: https://jsfiddle.net/holgerl/q0z979uy/

Google Maps JS API Fit Bounds Not Working

I'm having a very strange problem with my Google Maps implementation. I am getting back an array from a REST service that gives me lat/lng and I am in turn using that information to put markers on the map and implement functionality.
That part works fine, it's when it comes to taking the markers output on the page and getting their bounds and zooming the map to them that I get to the problem. I have read several solutions and I have attempted implementing them to the best of my ability.
Here's my implementation:
// Reset the maps bounds
MapView.bounds = new google.maps.LatLngBounds(null);
// Get the new map bounds
for (var i=0, j=MapView.markers.length; i<j; i++){
var marker = MapView.markers[i],
lat = marker.position.lat(),
lng = marker.position.lng(),
latlng = new google.maps.LatLng(lat, lng);
MapView.bounds.extend(latlng);
};
// Set the map bounds to the new bounds
MapView.map.fitBounds(MapView.bounds);
Here is a list of the coordinates being used:
21.245445,-105.167631
41.887668,-87.622522
49.817492,15.472962
50.075538,14.4378
33.951611,-118.387578
41.887668,-87.622522
14.782827,-90.793702
33.741973,-78.817013
45.922225,-95.408973
28.320306,-81.422964
36.166667,-86.783333
-17.82922,31.053961
-8.708704,115.169616
41.901514,12.460774
-34.013717,23.054811
20.483443,-86.971039
34.933333,34.083333
6.428055,-9.429499
38.79142,-95.960607
43.771033,11.248001
46.271588,13.95641
33.773,-78.779504
40.789342,-3.249749
20.926822,-156.695125
46.271588,13.95641
18.853921,-71.300939
36.462205,-5.011611
25.788969,-80.226439
50.36809,8.73632
37.540667,126.948346
45.495992,-121.5879
14.782827,-90.793701
0,0
13,-76
20.431006,-86.908065
40.75694,-73.984872
64.143935,-21.934099
-17.816667,25.15
12.879721,121.774017
32.640054,-117.084196
-16.522046,28.850942
40.280559,22.50584
39.202686,-106.831683
36.122611,-115.170973
38.79142,-95.960607
18.126285,-65.440099
22.876396,-109.918562
30.36884,-86.324846
36.076518,-115.153343
36.0443,14.251222
41.894809,-87.624214
36.0443,14.251222
-34.035086,23.046469
36.42,25.431667
-17.82922,31.053961
20.696686,-105.292631
18.533333,-68.366667
42.407211,-71.382437
21.158964,-86.845937
9.748917,-83.753428
14.782827,-90.793701
34.852965,32.361479
34.939737,32.461585
34.052234,-118.243685
64.143935,-21.934099
35.369598,24.482727
52.407927,3.222711
47.497912,19.040235
21.158964,-86.845937
37.446719,25.328862
21.160386,-86.843338
39.770247,21.182861
36.124253,-115.168476
46.421684,15.856075
116.468401,39.947856
41.553221,-70.608589
43.706449,7.292265
39.415044,21.737618
21.158964,-86.845937
20.629559,-87.126904
36.832012,25.897065
50.075538,14.4378
8.87509,98.352656
57.702051,11.982304
-24.183889,29.012778
38.904253,-77.047904
0,0
-25.360413,27.09947
55.940209,-3.225319
36.286023,-5.27918
-3.386069,39.971999
20.749045,-105.31098
17.280151,-62.689038
21.040195,-104.358146
14.782827,-90.793702
20.696686,-105.292631
50.071287,14.397221
51.891877,-8.493827
Then I used those bounds to create a poly line to try and find out where the problem was, and the wierd part is even that is wrong, but the map isn't even zooming/panning to THOSE bounds.
Code for poly line:
var ne = MapView.bounds.getNorthEast();
var sw = MapView.bounds.getSouthWest();
var boundingBoxPoints = [
ne, new google.maps.LatLng(ne.lat(), sw.lng()),
sw, new google.maps.LatLng(sw.lat(), ne.lng()), ne
];
var boundingBox = new google.maps.Polyline({
path: boundingBoxPoints,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
boundingBox.setMap(MapView.map);
And a picture of the result (this is as the map loads, un-altered):
It will either load like that (if I'm lucky) or load with most of the trips out of the top of the map with only a couple showing and most of the map canvas gray.
I am a total loss right now, could somebody please point me in the right direction?

Openlayers marker size

I have an openlayers map with markers added as geometry vector points. In the style option I set a size for each. However, the problem is, that if I zoom in or zoom out, they all become the same size until I load the entire page again. In other words, once I zoom in or out, they are all the same.
var layer_style = OpenLayers.Util.extend({},
OpenLayers.Feature.Vector.style['default']);
var style = OpenLayers.Util.extend({}, layer_style);
var pointLayer = new OpenLayers.Layer.Vector("Point Layer");
map.addLayers([terrain, road, satellite, hybrid, pointLayer]);
var lonlat = new OpenLayers.LonLat(0, 140);
lonlat.transform(proj, map.getProjectionObject());
map.setCenter(lonlat, 2);
var point = new OpenLayers.Geometry.Point(-40, -40);
point = point.transform(proj, map.getProjectionObject());
style.pointRadius = 10;
var pointFeature = new OpenLayers.Feature.Vector(point, null, style);
pointLayer.addFeatures([pointFeature]);
var point = new OpenLayers.Geometry.Point(-40, -40);
point = point.transform(proj, map.getProjectionObject());
style.pointRadius = 40;
var pointFeature = new OpenLayers.Feature.Vector(point, null, style);
pointLayer.addFeatures([pointFeature]);
When I load this, I get two markers, one size 10, the other 40. But when I zoom in or out, they all become same size.
You are overwriting the pointRadius property of the style object each time, so in the end the last value will be used as OpenLayers will only point to the style.
What you need to do is use a lookup to let the pointRadius depend on a given feature attribute.
See Rule-based Styling: http://trac.osgeo.org/openlayers/wiki/Styles#Rule-basedStyling

How can I increase a GLatLngBounds (rectangle) by 10% in google maps API?

I have a map with a bunch of points on it, and I want to zoom such that all points fit on the screen. The code for this is fairly simple:
var bounds = new GLatLngBounds();
for (var n = 0; n < points.length; n++) {
bounds.extend(points[n].getLatLng());
}
zoomLevel = map.getBoundsZoomLevel(bounds);
to get the zoom level, you have to pass in a bounds object. To create a bounds object, you either give it two points like a rectangle, or you pass it a point and it increases in size (if necessary) to incorporate the point which is what I do above.
The problem is that the points appear right on the edge of the screen. I'd like to increase the size of the bounds by say, 10%. I can get the two points out of my bounds object using bounds.getNorthEast(); and bounds.getSouthWest(); but to increase them by 10% of the size of the bounds is proving tricky. I tried toSpan() on the bounds which gives you a LatLng containing the size of the bounds, and then did 10% of the Lat and Lng and added it to my coordinates. However, it did not seem to work. My code for this attempt was:
var pointNorthEast = bounds.getNorthEast();
var pointSouthWest = bounds.getSouthWest();
var latAdjustment = ((pointNorthEast.lat() - pointSouthWest.lat()) * increasePercentage) / 2;
var lngAdjustment = ((pointNorthEast.lng() - pointSouthWest.lng()) * increasePercentage) / 2;
var newPointNorthEast = new GLatLng(pointNorthEast.lat() + latAdjustment, pointNorthEast.lng() + lngAdjustment);
var newPointSouthWest = new GLatLng(pointSouthWest.lat() - latAdjustment, pointSouthWest.lng() - lngAdjustment);
bounds = new GLatLngBounds();
bounds.extend(newPointNorthEast);
bounds.extend(newPointSouthWest);
By my reckoning, bounds should then be 10% bigger. I'm not certain it represents just a rectangle or stores any coordinate data or not so I didn't take chances by adding/subtracting half of the increase on each side to keep it centered.
However, trying various increasePercentage values such as 1.1 or 1.5 it does not seem to make a difference.
Therefore I've either cocked up my logic, my maths, or the fact that zoomLevel is not a smooth scale is hampering my attempts.
I think I might have to just do zoomLevel -= 1 and have done with it, but I wanted to see if anyone had any ideas first.
here's updated code using current api version (v3.0):
var increasePercentage = 1.10; //10%
var pointSouthWest = mb.getSouthWest();
var latAdjustment = (pointNorthEast.lat() - pointSouthWest.lat()) * (increasePercentage - 1);
var lngAdjustment = (pointNorthEast.lng() - pointSouthWest.lng()) * (increasePercentage - 1);
var newPointNorthEast = new google.maps.LatLng(pointNorthEast.lat() + latAdjustment, pointNorthEast.lng() + lngAdjustment);
var newPointSouthWest = new google.maps.LatLng(pointSouthWest.lat() - latAdjustment, pointSouthWest.lng() - lngAdjustment);
bounds = new google.maps.LatLngBounds();
bounds.extend(newPointNorthEast);
bounds.extend(newPointSouthWest);
map.fitBounds(bounds);
The problem is in your Math, here's how you need to calculate adjustments:
var latAdjustment = ((pointNorthEast.lat() - pointSouthWest.lat()) * (increasePercentage - 1);
var lngAdjustment = ((pointNorthEast.lng() - pointSouthWest.lng()) * (increasePercentage - 1);

Flex/actionscript snapshot with clipping rectangle and scaling matrix

var snapshot:ImageSnapshot = ImageSnapshot.captureImage(someSprite);
var file:FileReference = new FileReference();
file.save(snapshot.data,'abc.png');
In the above code I am able to capture an image.
But I also want to apply a scalingMatrix(for zoomIn/Out) and a clipping rectangle to it.
How to do it?
I tried capturebitmapdata too, but with that I can't even get a proper image. See here. So I don't want to use that.
sw = someSprite.stage.stageWidth;
sh = someSprite.stage.stageHeight;
var cr:Rectangle = new Rectangle(x,y,cw,ch);//you have to check that this clip rectangle should not overshoot your stage
//cr is the clip rectangle
var bmp:BitmapData = new BitmapData(sw,sh);
bmp.draw(someSprite,null,null,null,cr);
var bmp1:BitmapData = new BitmapData(cw,ch);
bmp1.copyPixels(bmp,cr,new Point(0,0));
var enc:JPEGEncoder = new JPEGEncoder();
var data:ByteArray = encoder.encode(bmd1);
new FileReference().save(data,'image.jpeg');
The above code allows you to draw only the portion inside the clip rectangle.
In my case I didn't have to take into account a scaling matrix, even though
I was using zoom In/Out features.

Resources