Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple problems #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions leaflet-pegman.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ L.Control.Pegman = L.Control.extend({
iconAnchor: [24, 33],
iconUrl: 'data:image/png;base64,' + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAFElEQVR4XgXAAQ0AAABAMP1L30IDCPwC/o5WcS4AAAAASUVORK5CYII=",
}),
}
},
// Setting this to false can save a lot of http requests (on every mouse move),
// but it will no longer show a mouse pointer when hovering a tile with content
downloadTiles: true
},

__interactURL: 'https://unpkg.com/[email protected]/dist/interact.min.js',
Expand Down Expand Up @@ -107,6 +110,10 @@ L.Control.Pegman = L.Control.extend({
},

onRemove: function(map) {
interact(this._pegman).unset();
interact(this._map._container).unset();
interact(this._container).unset();

if (this._googleStreetViewLayer) this._googleStreetViewLayer.remove();
if (this._pegmanMarker) this._pegmanMarker.remove();

Expand Down Expand Up @@ -286,7 +293,7 @@ L.Control.Pegman = L.Control.extend({
},

onPanoramaPositionChanged: function() {
var pos = this._panorama.getPosition();
var pos = this._getOrCreatePanorama().getPosition();
pos = L.latLng(pos.lat(), pos.lng());
if (this._map && !this._map.getBounds().pad(-0.05).contains(pos)) {
this._map.panTo(pos);
Expand All @@ -295,7 +302,7 @@ L.Control.Pegman = L.Control.extend({
},

onPanoramaPovChanged: function() {
var pov = this._panorama.getPov();
var pov = this._getOrCreatePanorama().getPov();
this._pegmanMarker.getElement().style.backgroundPosition = "0 " + -Math.abs((Math.round(pov.heading / (360 / 16)) % 16) * Math.round(835 / 16)) + 'px'; // sprite_height = 835px; num_rows = 16; pegman_angle = [0, 360] deg
},

Expand Down Expand Up @@ -380,13 +387,16 @@ L.Control.Pegman = L.Control.extend({
processStreetViewServiceData: function(data, status) {
if (status == google.maps.StreetViewStatus.OK) {
this.openStreetViewPanorama();
this._panorama.setPano(data.location.pano);
this._panorama.setPov({

var panorama = this._getOrCreatePanorama();

panorama.setPano(data.location.pano);
panorama.setPov({
heading: google.maps.geometry.spherical.computeHeading(data.location.latLng, this._streetViewCoords),
pitch: 0,
zoom: 0
});
this._panorama.setVisible(true);
panorama.setVisible(true);
} else {
console.warn("Street View data not found for this location.");
// this.clear(); // TODO: add a visual feedback when no SV data available
Expand Down Expand Up @@ -447,18 +457,28 @@ L.Control.Pegman = L.Control.extend({
this._googleStreetViewLayer = L.gridLayer.googleMutant(this.options.mutant);
this._googleStreetViewLayer.addGoogleLayer('StreetViewCoverageLayer');

this._panorama = new google.maps.StreetViewPanorama(this._panoDiv, this.options.pano);
this._streetViewService = new google.maps.StreetViewService();

this._panorama.addListener('closeclick', L.bind(this.onStreetViewPanoramaClose, this));
this._panorama.addListener('position_changed', L.bind(this.onPanoramaPositionChanged, this));
this._panorama.addListener('pov_changed', L.bind(this.onPanoramaPovChanged, this));

if (toggleStreetView) {
this.showStreetViewLayer();
}
},

_getOrCreatePanorama: function() {
if (this._panorama != null) {
return this._panorama;
}

this._panorama = new google.maps.StreetViewPanorama(this._panoDiv, this.options.pano);

this._panorama.addListener('closeclick', L.bind(this.onStreetViewPanoramaClose, this));
this._panorama.addListener('position_changed', L.bind(this.onPanoramaPositionChanged, this));
this._panorama.addListener('pov_changed', L.bind(this.onPanoramaPovChanged, this));

return this._panorama;
},

_initMouseTracker: function() {
if (!this._googleStreetViewLayer) return;

Expand Down Expand Up @@ -528,7 +548,7 @@ L.Control.Pegman = L.Control.extend({
},

_downloadTile: function(imageSrc, callback) {
if (!imageSrc) return;
if (!imageSrc || !this.options.downloadTiles) return;
var img = new Image();
img.crossOrigin = "Anonymous";
img.addEventListener("load", callback.bind(this, img), false);
Expand Down
Loading