I am building a phonegap application and I have one input element like this:
<input type="text" id="currLocation"></input>
And I have a Javascript code like this in bottom of the page:
$(document).ready(function(){
geocoder = new google.maps.Geocoder();
navigator.geolocation.getCurrentPosition(onSucc, onFail,{timeout: 10000, enableHighAccuracy: true});
}
To get the current location of a user. This code works fine, but the only problem I am facing is page freezes until getCurrentPosition() completes load(success or failure) and entire page displays as white. What I want is at-least DOM elements should display and this getCurrentPosition() should execute in background so UI won't freeze. How do I achieve this? I tried doing like this by calling those without functions in full bottom of the page:
<script>
geocoder = new google.maps.Geocoder();
navigator.geolocation.getCurrentPosition(onSucc, onFail,{timeout: 10000, enableHighAccuracy: true});
<script>
But I get below error:
Uncaught TypeError: Failed to execute 'getCurrentPosition' on 'Geolocation': The callback provided as parameter 1 is not a function
Can anyone please guide me in this?
Related
I am not sure if many developers have looked into the new PiP API from Google Chrome v70 but I need some help. I am trying to replace the video source on resize. Why? So I can give lower or higher resolution video's based on the PiP window size. Effectively creating a responsive PiP. However, I have run in multiple problems.
1 - I can only call a PiP on events fired by the user, meaning I can't open a PiP window based on a callback. Error: Uncaught (in promise) DOMException: Must be handling a user gesture to request picture in picture.
2 - I need to somehow wait until the new source is loaded, otherwise, I get the following error: Uncaught (in promise) DOMException: Metadata for the video element are not loaded yet.
In there demo they even suggest loading a smaller video on resize. Has anyone done this? Can anyone help me?
videoElement.addEventListener('enterpictureinpicture', function(event) {
pipWindow = event.pictureInPictureWindow;
pipWindow.addEventListener('resize', onPipWindowResize);
});
videoElement.addEventListener('leavepictureinpicture', function() {
pipWindow.removeEventListener('resize', onPipWindowResize);
});
function onPipWindowResize() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(async function() {
await document.exitPictureInPicture();
videoElement.setAttribute('src', './video-sd.mp4');
await videoElement.requestPictureInPicture();
}, 1000);
}
I'm trying to load custom photospheres inside of map pin descriptions using the google maps streetview api.
It loads correctly the first time, but when I attempt to open either the same map pin or another, (which sets the panoimg variable and recalls initPano()), I get a blank image. And in the console, a failed 400 error to the URL https://geo0.ggpht.com/cbk?cb_client=apiv3&panoid=custom&output=tile&x=0&y=0&zoom=0&nbt&fover=2
I've got an my code here on JSfiddle
https://jsfiddle.net/falldeaf/9ej8x2xj/1/
(JSfiddle stripped down further to NOTHING except the problem described)
I"m trying to call this function each time a map pin is opened:
function initPano() {
var panoOptions = {
pano: 'custom',
visible: true,
panoProvider: getCustomPanorama
};
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('pano-canvas'), panoOptions);
}
function getCustomPanoramaTileUrl(pano, zoom, tileX, tileY) {
console.log(panoimg);
return "https://www.tcpalmextras.com/tcpalm_hosted/images/brandyhallmap/" + panoimg;
}
You can see the error occur by clicking the map pin on the screen, then closing the popup, then opening it again. This second and all subsequent openings don't properly load the photosphere.
If you're creating the custom photospheres inside of modal windows, do NOT add visible:true option present in the examples to the new google.maps.StreetViewPanorama object's options.
Removing that line allows them to open an indefinite number of times.
I really hope someone can help with my problem. I have built a mobile web app http://ufa-ld-qa.azurewebsites.net/ (the QA site) with asp.net mvc4 using Bing Maps API for various functionality in the app. I am having problems with the directions module. When I view the site on my pc (Chrome and IE) it works fine and I see no errors but on mobile devices it is not working (but it did work fine yesterday when we launched to QA). I have used HTML5 geolocation (this may be the issue) to get user's location to allow them to get directions to a location. I will post my code below and if anyone could please help me it would be greatly appreciated. We have tested it on about 7 different mobile devices with different OS's and it doesn't work on any. Does anyone know if this is a Bing issue or my code below? Thanks so much in advance.
<script type="text/javascript">
var map = null;
var directionsManager = null;
var userLat = null;
var userLong = null;
var userPosition = null;
var latlng = new Microsoft.Maps.Location(#Model.latitude, #Model.longitude);
navigator.geolocation.getCurrentPosition(locationHandler);
function locationHandler(position)
{
userPosition = new Microsoft.Maps.Location(position.coords.latitude, position.coords.longitude);
}
function GetMap() {
// Initialize the map
map = new Microsoft.Maps.Map(document.getElementById("map"), { credentials: "Au_7giL-8dUbFkJ8zLjcQKy4dV2ftPfpMxQ0_sVBksoj4Y-1nBT00Z1oqUIU894_",
mapTypeId: Microsoft.Maps.MapTypeId.road});
Microsoft.Maps.loadModule('Microsoft.Maps.Directions', { callback: directionsModuleLoaded });
}
function directionsModuleLoaded() {
// Initialize the DirectionsManager
directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
// Create start and end waypoints
var startWaypoint = new Microsoft.Maps.Directions.Waypoint({ location: userPosition });
var endWaypoint = new Microsoft.Maps.Directions.Waypoint({ location: latlng });
directionsManager.addWaypoint(startWaypoint);
directionsManager.addWaypoint(endWaypoint);
// Set request options
directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });
// Set the render options
directionsManager.setRenderOptions({
itineraryContainer: document.getElementById('directionPanel'),
displayWalkingWarning: false,
walkingPolylineOptions: { strokeColor: new Microsoft.Maps.Color(200, 0, 255, 0) },
});
// Specify a handler for when an error occurs
Microsoft.Maps.Events.addHandler(directionsManager, 'directionsError', displayError);
// Calculate directions, which displays a route on the map
directionsManager.calculateDirections();
}
function displayError(e) {
// Display the error message
alert(e.message);
}
</script>
A couple of things to try. First ensure that your app is allowed to access the users location. Most mobile platforms require you to mark that the app requires access to the GPS in the manifest. Another thing to look into is the possibility that the userLocation isn't populated before your callback for the directions manager is called. It's possible that the GPS takes a little longer on the mobile device to find the users location and as such the directions loaded function is firing before the users location is set, thus passing in a null starting . You might find it useful to have a flag to indicate that the directions manager has loaded and a simple function that runs after setting the flag and also runs after setting the use location that checks that both the directions manager has loaded and the user location has been set and then calls your directions loaded function.
My Windows Phone 8 App is experiencing similar behavior. (Nokia 920)
http://bing.com/maps/default.aspx?cp=47.677797~-122.122013&lvl=12
When the Website preference is set to 'desktop version' the map renders correctly.
When the Website preference is set to 'mobile version' the map renders incorrectly.
Just started happening about a week ago !
Trying to load the Google maps asynchronously so that the start of the HTML page is faster.
Have used the following code as mentioned in the link: https://developers.google.com/maps/documentation/javascript/tutorial#asynch. Section: Asynchronously Loading the API.
function initialize() {
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=TRUE_OR_FALSE&callback=initialize";
document.body.appendChild(script);
}
window.onload = loadScript;
Issues:
When the network is disconnected after the loadScript() function is invoked. Currently we do not find anyway to check the network connection is terminated & do appropriate error handling.
Even when the network is available some times the callback function 'initialize()' is never called.
Loading of google maps on mobile device is very slow even on a 3G network.
In both the cases mentioned above, we start a loading icon immediately after loadScript() is invoked. As initialize() is not called back it gets blocked in the loading icon for infinite time.
Kindly provide some pointers to handle the network disconnect & callback function not invoked issues.
The Maps API does not have a native way to handle this. Thankfully HTML5 does. Listening to the document.online / document.offline events is very effective and I have used it to solve this problem.
See https://developer.mozilla.org/en/DOM/document#Event_handlers
This is supported in at least iOS 5 and Android 2.3.
You could also use setTimeout to check that the API callback has occurred and handle it appropriately if not.
I have a page that renders a simple google map for a specified location. This map renders without any problems at all when I run it locally on localhost, however, when I deploy this code to our live web servers (using our LIVE google API key for the appropriate domain) it fails to render, and upon putting a series of alerts within the javascript on the page, it appears that the 'Initialize' method (which should be called within body onLoad) is not being called.
When I view the HTML source that is rendered on the live server it appears exactly as per the local version of the site (including the call to initialize() within the body onLoad event), albeit with the different maps API key.
I have output the host (alert(window.location.host);) to ensure that the key I generated via the google maps api site, corresponds exactly to the live server, which it does.
Does anyone have any ideas why it would be working locally but not when deployed to the live servers? The live site is hosted on 2 load-balanced web servers.
This is the javascript that is rendered:
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAA-BU8POZj19wRlTaKIXVM9xTz76xxk4yAELG9u79oXrhnLTB5NRRvAZ-bkKn1x8J68nfRTVOIWNPJEA" type="text/javascript"></script>
<script type="text/javascript">
var map;
var geocoder;
alert(window.location.host);
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("businessMap"));
map.setUIToDefault();
geocoder = new GClientGeocoder();
showAddress('St Margarets Street SW1P 3 London');
}
}
function showAddress(address) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
// Address could not be located.
jQuery('#googleMap').hide();
} else {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
var html = 'Address info for the marker';
marker.openInfoWindow(html);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
}
}
);
}
</script>
Any help would be much appreciated.
Thanks.
I've located the problem. It was some conflicting javascript related to Facebook Connect that was calling an FB Connect related page on window.onload, therefore not calling the initialize function within the body onload event.
I should have noticed this conflicting javascript earlier!