I am trying to create an application using Ionic2 that allows a user to load a StreetViewPanorama object through the Google Street View API. After loading the view, the user should be able to manipulate the street view in any way they choose (moving away from the original position, scaling, etc.). Upon completion of this task, the user captures a static image of the final street view.
My difficulty arises when I try to capture a photo of a new street location. I am trying to use google static image documentation to achieve this . Unfortunately, I cannot get a link to the attributes of the Panorama object after creating the object. I'm relatively new to Javascript, so bear with me.
To create a street view panorama, I run the following functions (starting from the bottom of initMap() ):
generatePanorama(userLocation): void { var streetviewService = new google.maps.StreetViewService; streetviewService.getPanorama({ location: userLocation, preference: google.maps.StreetViewPreference.NEAREST, radius: 100}, function(result, status) { console.log("Adjusted latitude: ", result.location.latLng.lat(), "\nAdjusted longitude: ", result.location.latLng.lng()); new google.maps.StreetViewPanorama(document.getElementById('street-view'), { position: result.location.latLng, pov: {heading: 165, pitch: 0}, zoom: 1 }); }); } getLocation(callback): void { Geolocation.getCurrentPosition().then((position) => { console.log("Latitude: ", position.coords.latitude, "\nLongitude: ", position.coords.longitude); callback({lat: position.coords.latitude, lng: position.coords.longitude}); }).catch((error) => { console.log('Error getting location', error); }); } initMap(): void { this.getLocation(this.generatePanorama); }
I create a panorama as shown above with the code,
new google.maps.StreetViewPanorama(document.getElementById('street-view'), { position: result.location.latLng, pov: {heading: 165, pitch: 0}, zoom: 1 });
I cannot assign this object to an instance variable for use in the following two functions:
generateStaticMapsURL(lat, lng, heading, pitch): string { var url = "https://maps.googleapis.com/maps/api/streetview?size=600x300&location="; url += lat + "," + lng; url += "&heading=" + heading; url += "&pitch=" + pitch; url += "&key=SECRET_KEY"; return url; } openShareModal() { console.log("Final Latitude: ", this.panorama.getPosition().lat()); console.log("Final Longitude: ", this.panorama.getPosition().lng()); console.log("Final Heading:", this.panorama.getPov().heading); console.log("Final Heading:", this.panorama.getPov().pitch); let myModal = this.modalCtrl.create(ShareModalPage); myModal.present(); }
When I try to assign an object to an instance variable either directly or through a helper function, I get UnhandledPromiseRejectionWarning and nothing works. So, how exactly can I extract objects such as location, title, and step from the street view object after it's created?
Thank you for your help!
Update 1: Currently the program is working fine. I assign an instance variable panorama: any; , and then proceed to try to update the variable using the following function and purpose.
generatePanorama(userLocation): void { var streetviewService = new google.maps.StreetViewService; streetviewService.getPanorama({ location: userLocation, preference: google.maps.StreetViewPreference.NEAREST, radius: 100}, function(result, status) { console.log("Adjusted latitude: ", result.location.latLng.lat(), "\nAdjusted longitude: ", result.location.latLng.lng()); this.panorama = new google.maps.StreetViewPanorama(document.getElementById('street-view'), { position: result.location.latLng, pov: {heading: 165, pitch: 0}, zoom: 1 }); }); }
When I do this and then try to use the panorama variable in another function, it seems that panorama is an empty variable. In addition, the panorama does not load at all! Here is the second function in which I am trying to use a panorama variable.
openShareModal() { console.log("Final Latitude: ", this.panorama.getPosition().lat()); console.log("Final Longitude: ", this.panorama.getPosition().lng()); console.log("Final Heading:", this.panorama.getPov().heading); console.log("Final Heading:", this.panorama.getPov().pitch); let myModal = this.modalCtrl.create(ShareModalPage); myModal.present(); }
UPDATE 2: Posting the entire snippet of my code for help.
import { Component, ViewChild } from '@angular/core'; import { IonicPage, NavController, NavParams, ViewController, ModalController } from 'ionic-angular'; import { ShareModalPage } from '../share-modal/share-modal'; import { Geolocation } from 'ionic-native'; declare var google; @IonicPage() @Component({ selector: 'page-street-view-modal', templateUrl: 'street-view-modal.html', }) export class StreetViewModalPage { @ViewChild('map') mapElement; map: any; panorama: any; constructor(public navCtrl: NavController, public navParams: NavParams, public viewCtrl: ViewController, public modalCtrl: ModalController) {} ionViewDidLoad() { console.log('ionViewDidLoad StreetViewModalPage'); this.initMap(); } generatePanorama(userLocation): void { var streetviewService = new google.maps.StreetViewService; streetviewService.getPanorama({ location: userLocation, preference: google.maps.StreetViewPreference.NEAREST, radius: 100}, function(result, status) { console.log("Adjusted latitude: ", result.location.latLng.lat(), "\nAdjusted longitude: ", result.location.latLng.lng()); this.panorama = new google.maps.StreetViewPanorama(document.getElementById('street-view'), { position: result.location.latLng, pov: {heading: 165, pitch: 0}, zoom: 1 }); }); } getLocation(callback): void { Geolocation.getCurrentPosition().then((position) => { console.log("Latitude: ", position.coords.latitude, "\nLongitude: ", position.coords.longitude); callback({lat: position.coords.latitude, lng: position.coords.longitude}); }).catch((error) => { console.log('Error getting location', error); }); } initMap(): void { this.getLocation(this.generatePanorama); } generateStaticMapsURL(lat, lng, heading, pitch): string { var url = "https://maps.googleapis.com/maps/api/streetview?size=600x300&location="; url += lat + "," + lng; url += "&heading=" + heading; url += "&pitch=" + pitch; url += "&key=XXXXXXXXXXXX";
And the corresponding HTML ...
<ion-content> <div #map id="street-view" style="height:100%; width:100%;"></div> <button ion-button style="position: absolute; top: 5px; right: 5px; z-index: 1;" (click)="openShareModal()" large><ion-icon name="camera"></ion-icon></button> </ion-content>