To get current zoom and pan values from svg-pan-zoom - svgpanzoom

I am using react js, to apply panning and zooming for map a svg image, i am initialising Svgpanzoom instance as mentioned below
after panning and zooming i need the exact values of zoom and pan. How can I get them?
let panZoomInstance = svgPanZoom('#selectorId')
panZoomInstance.zoom(0.8)
panZoomInstance.pan({ x: 0, y: 0 })

You can use getPan() and getZoom() functions for getting the current values of Pan and Zoom.

Related

Unable to set pan and zoom without a visual gap

Using svg-pan-zoom utility, I want to register the zoom and pan values every time is changes (using onPan() and onZoom()) and use these saved values to pan and zoom my SVG to the same position (using pan() and zoom()).
I works fine if the zoom level is not changed, however, if zoom level is changed, I have a gap between the wanted position of the svg and the real one.
You can see this problem in that fiddle: first, zoom in, then press the Pan button.
I would like my svg to keep its current location.
I have read other posts on stackoverflow about similar situations (I guess I should use data from getSizes()) but I'm still unable to make it work.
Any advice?
OK, I got the solution, which is quite obvious.
While zooming, I need to apply a zoom factor to the saved pan.
onZoom: function(zoom) {
ratio = zoom/currentZoom;
currentZoom = zoom;
currentPan = {
x: currentPan.x*ratio,
y: currentPan.y*ratio
}
}
See full code here

Get rotation in Cesium

I want to know the rotation angle from Cesium when I turn the map using ctrl+mouseLeft. Like I did on this image:
I tried viewer.camera.roll but doesn't seem to be right. It's always zero.
Any tips in how I can get that value in 2D and 3D? I would also like to set this value.
Thanks in advance!
EDIT:
I have used the solution suggested and now I'm getting different values (in radians). It works both on 2D and 3D maps.
EDIT2: How to set the rotation to an specific angle
setRotation(angle: number): void {
viewer.camera.setView({
heading: (angle / (180 / Math.PI)) // east, default value is 0.0 (north)
})
};
Based on this doc
EDIT 3:
I'm using the following code to get the current heading of the map:
public getAngle(): number {
return viewer.camera.heading;
}
When I call this function and my map is not rotated, as the image shows, I get the result of "6.283185307179586" radians. I thought it should be zero, because it's not rotated at all. If I move the map around with the mouse (pan) and call the getAngle function again it gives differents results, as "1.4612025367455317e-8" if I move it north. Any thoughts about it? I would like to get the heading of the map.
Thanks a lot!
This value is available from viewer.camera.heading and is expressed in radians. You can twist the camera like this using the twistLeft and twistRight functions on the camera.

panToBounds don't adjust zoom

I have following problem, i use autocomplete to get coordinates for needed places, i has use before Autocomplete.getPlace().geometry.location for map.setCenter() and have manual make maximum zoom. But i have found, that it given Autocomplete.getPlace().geometry.viewport, how i can it understand, it give me 4 X,Y points for corners, so i have put it inside of map.panToBounds(). I dont know how, but the map will slide to the right viewport, but with wrong zoom level . Before i start to type in autocomplete, the zoom is on 19, and after i fire action, the map slide to the right location, but position the map Bounds not exactly to autocomplete viewport, it zoom it on the center.
var autocomplete = new google.maps.places.Autocomplete(my_input,{types:['geocode']});
google.maps.event.addListener(autocomplete, 'place_changed', function()
{
my_map.panToBounds(this.getPlace().geometry.viewport);
});
Self fixed, i dont know for what is map.panToBounds(), but what i needed was map.fitBounds()

Markers With Fixed Real (not on-screen) Radius and MarkerClusterer

I'm attempting to create a map that contains markers of a fixed real radius in metres. I'm using the CIRCLE symbol style but attempting to change the size of the circle using the scale option doesn't appear to be much the right way to go because it stays the same size on-screen regardless of the zoom level.
So, for example, I might want a 500m radius circle at a specific point and use:
var markerOptions = {
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 500
},
position: new google.maps.LatLng(0, 50)
};
marker = new google.maps.Marker(markerOptions);
But the marker size doesn't change size when I change zoom, staying the same number of pixels across regardless of zoom level.
I tried using google.maps.Circle as an alternative to google.maps.Marker, and although Circle has a radius option and that works just fine on the scaling side of things I can't use MarkerClusterer to group them together when there are lots of them on-screen at the same time, which is another requirement.
So how do I make both clustered marker groups and fixed-size markers work together?
Thanks to the suggestion from #geocodezip I took a look at the source code for MarkerClusterer and managed to get it working with circles with a few tweaks. In case anyone else needs to do something similar here are the changes that I made to markercluster.js:
Changed all occurrences of getPosition() to getCenter()
Changed MarkerClusterer.prototype.pushMarkerTo_() to the following:
MarkerClusterer.prototype.pushMarkerTo_ = function (marker) {
// If the marker is draggable add a listener so we can update the clusters on the dragend:
marker.isAdded = false;
this.markers_.push(marker);
};
And now everything works using google.maps.Circle and MarkerClusterer.

Google Maps API V3: Zoom out breaks pan limitation; workaround?

function initialize() {
var latlng = new google.maps.LatLng(37.7702429, -122.4245789);
var myOptions = {
zoom: 3,
center: latlng,
disableDefaultUI: false,
mapTypeId: google.maps.MapTypeId.TERRAIN,
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
// Limit panning
// Latitude bounds for map, longitude not important
var southWest = new google.maps.LatLng(-85.000, -122.591);
var northEast = new google.maps.LatLng(85.000, -122.333);
var allowedBounds = new google.maps.LatLngBounds(southWest, northEast);
// Add a move listener to restrict the bounds range
google.maps.event.addListener(map, "center_changed", function() {checkBounds(); });
//If zoom out at bound limit then map breaks; center doesn't change but bounds get broken. Listen for zoom event and try to correct bound break. **Doesn't Work**
google.maps.event.addListener(map, 'zoom_changed', function() {checkBounds(); });
// If the map position is out of range, move it back
function checkBounds() {
// Perform the check and return if OK
if ((allowedBounds.getNorthEast().lat()>(map.getBounds().getNorthEast().lat()))&&(allowedBounds.getSouthWest().lat()<(map.getBounds().getSouthWest().lat()))) {
lastValidCenter = map.getCenter();
lastValidZoom = map.getZoom();
return;
}
// not valid anymore => return to last valid position
map.panTo(lastValidCenter);
map.setZoom(lastValidZoom);
}
}
Basically I don't want the user to be able to see anything outside of the map, so I have restricted the latitudinal bounds. Works normally.
The issue is that if a user we to be viewing close to the bound limits and then zooms out so that the center doesn't change, but now the view-port bounds are outside of the bound limit, it does not correct and the map becomes unpannable.
Any help you geniuses can offer is mucho appreciated .
Ok, so it was very simple. The zoom_changed event was not behaving as expected and bounds_changed on it's own was not satisfactory. This map will not go out of bounds by pan or zoom and is perfect for if you want the user to only see map and no grey background. Not so good if your users want to center the map at a high latitude and low zoom level. Cross that bridge later. Here's the code:
// The allowed region which the whole map must be within
var southWest = new google.maps.LatLng(-85.000, -122.591);
var northEast = new google.maps.LatLng(85.000, -122.333);
var allowedBounds = new google.maps.LatLngBounds(southWest, northEast);
// Add listeners to trigger checkBounds(). bounds_changed deals with zoom changes.
google.maps.event.addListener(map, "center_changed", function() {checkBounds(); });
google.maps.event.addListener(map, 'bounds_changed', function() {checkBounds(); });
// If the map bounds are out of range, move it back
function checkBounds() {
// Perform the check and return if OK
if ((allowedBounds.getNorthEast().lat()>(map.getBounds().getNorthEast().lat()))&&(allowedBounds.getSouthWest().lat()<(map.getBounds().getSouthWest().lat()))) {
lastValidCenter = map.getCenter();
lastValidZoom = map.getZoom();
return
}
// not valid anymore => return to last valid position
map.panTo(lastValidCenter);
map.setZoom(lastValidZoom);
don't try this on a working application or one thats in production make a new map somewhere
local server different test program whatever.
PANING google maps has always been buggy especially around the edges or close to any of the google icons for pan zoom etc .
you need to get back to basics forget functions and scripting just ask simple IF statements
similar to this CAN'T REMEMBER WHAT I DONE A COUPLE OF YEARS AGO bit the gist is this.
1) store 4 variables to represent four lines on a map
left edge west
right edge east
top edge north
bottom edge south
another to keep track of centre LAT LNG( the start centre same as first map displayed on load )
i,e if you pan left or west by X you make the centre / center (for americans) equal to current centre by plus the pan X ditto for all the other pan directions add or subtract the required latitudes and longitudes
test that you have not reached an edge (minus whatever by test trial and error) amount causes the google bug to appear close to icons around edge
or put another way make the LAT's and LNG's you are testing for to be smaller that the displayed map
if left or west edge of displayed map is at longitude x stop panning (by not doing anymore
adjustments s to your centre VARIABLE at X - 10 longitudes same for all other 3 edges by plus or minus the required amount
top edge north will will at lattitude a
bottom edge south will be at lattitude a - whatever you chose
left edge west will be at longitude b
right edge east will be at longitude b- whatever you chose
they will all be within the displayed map but say 0.5 of a latitude or longitude than displayed map
if your pan crosses any of your fine lines set you centre var back or forward and you pan back or forward by say 0.1 to o.5 of a latitude or longitude viewers will just see a slight jerk back into place when they pan outside your pre de=fine four lines across the map.
Code will like like very basic learner code but it will work.
at least it did for me after some trial and error and the usual debugging typo errors etc.
taking this approach you are in control and not google scripts that may have undocumented code in them that causes the problems your experiencing because google cannot know what your trying to achieve only you know that.
SORRY HAVE NOT GOT THE CODE ANYMORE its not difficult just looks like newbie code and not the code advanced users prefer
it's like making a simple learner program where if you move up or down or diagonally you adjust centre LAT LNG in you var thats store current centre (this is your own variable not a google variable ) if centre (stored in your own variable ) crosses one of your lines stop panning and set Gmap center back forward up or down by a little thats plus or minus a fraction of a LAT or LNG
Thanks, your script crashed my Mozilla :-) It is probably because the zoom_changed event doesn't give you correct center in getCenter() call. It is a known problem that zoom_changed event handler doesn't give correct bounds, so it may apply to the center too. I tried to modify your code to handle only the bounds_changed event (instead zoom_changed and center_changed), and the problem dissapeared! But it is not behaving ideally either.
Maybe better look at this example of range limitation, it works quite nicely.