Google maps javascript api services crashing in phonegap when connection is unstable - javascript

Unstable connection in a normal browser is not a major issue because the user can click on the "reload button" if the page didn't load properly. In phonegap there is no such button, so we have to make sure everythings loads correctly 100% of the time.
I experience a problem because when connection is unstable during the initialisation of a google service (places autocompletion, geocoding or map display), no error message is thrown and yet the service will NOT work.
First we initialize google services
//In this senario the internet connection is good and stable
$('body').append('<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&v=3.exp&callback=googleCallback"></script>');
Then we want to use the google places autocomplete service and here is a scenario that is reproducible :
//At this point we lose the internet connection
var autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'));
//At this point autocomplete is defined and not empty, no error thrown
//At this point the internet connection come back
//At this point the autocomplete box is not working
Note that this "bug" also occurs for geocoding and map display.
The first part of my question is : how can i at least detect in my code that the initialisation of the (autocomplete or geocoding or display map) service didn't work properly.
Now we would like to reload the places autocomplete because internet is back and we need the service
//From now on the connection is good and stable
var autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'));
//We get no error but the autocomplete box still does not work
Here is the only way i found to get the places autocomplete service to work after connection loss occured as shown in scenario 1
//From now on the connection is good and stable
$('body').append('<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&v=3.exp&callback=googleCallback"></script>');
var autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'));
//At this point the autocomplete box is working properly
So the second part of my question is, do i really have to reload the hole google maps javascript api when 1 particular service failed to initialize ?
To reproduce the bug you can follow that link, it is referenced as problem 3.
If you're intersted in problem 1 you can check this question
If you're intersted in problem 2 you can check this question

Related

Google Recaptcha Error (The registered key does not work with this interface.)

I am a newbie so I dont know where the error has ocurred.
I used Invisible Google Recaptcha and every syntax is correct but google is throwing me an error that The registered key does not work with this interface. Please Help
Click Here to see a pic of the error! Error Image
You're getting this error when:
your keys are not white-listed by Google for the new Invisible reCaptcha api
your keys were just white-listed by Google but are not synchronized yet through all their servers
you didn't register your domain with the keys (you're requesting the Invisible reCaptcha api from an unregistered domain: localhost or any other)
I had this same issue. My issue was that I had created recaptcha keys, not "Invisible recaptcha" keys. Make sure you select the right option when creating the keys in the first place!

How to explore more about the APT provided in the Firefox Add-on SDK (jpm)?

I am presently working on an add-on that processes the text selection made by the user. For details I referred to Selection API Documentation - MDN and found the method to start logging the text selected by the user at console.
But steps that need to be done to stop the process of this selection logging is not described. Thus I need help to know how to stop the selection logging at console and more about this API but am unable to find any relevant resource to do the same.
The code to initiate selection is as follows :-
var selection = require("sdk/selection");
function myListener() {
if (selection.text)
console.log(selection.text);
}
selection.on('select', myListener);
Need to stop Logging without restarting the browser

Google doesn't tell me which waypoint is wrong (Javascript API)

When Google returns a NOT_FOUND response to a direction request with waypoints, the documentation says
NOT_FOUND indicates at least one of the locations specified in the requests
origin, destination, or waypoints could not be geocoded.
But it doesn't tell me which one is invalid...
I realise I could then attempt to geocode each address individually to find the bad address, but that is wasting a lot of geocoding requests, given I have about 20 waypoints, and given Google could just tell me which one failed...
Is there a way to do this?
My fallback is to geocode the addresses before I save them, however I'd prefer not to do this, if I can get google to tell me which waypoint failed.
Normally, the response comes back with warnings, and I thought the bad address may come back in that, however I think that only happens if a route can be built in the first place.
Warning items are intended to display information when a route has been found, you are right. For example, it is telling that the walking directions mode results are in beta.
Sample: http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charle,MA&mode=walking
For the problem you depicted (waypoint is not found), the response of the webservice is really short. Sample call with a non-working waypoint:
http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Chle,MA
Result:
{
"routes" : [],
"status" : "ZERO_RESULTS"
}
The google maps Directions API is not currently helping for your case, and you also cannot try to geocode all your waypoint at once. Furthermore, the geocode api will throw an error if you exceed 5 simultaneous requests, so you initial workaround idea may be limited.
As a workaround (but not perfect, as the results between this service and gmaps service can be different), you could try to use other geocoding services, such as the one provided by OpenStreetMap: http://wiki.openstreetmap.org/wiki/Nominatim#Search
You still have to do it one-by-one, but you won't be getting a "query limit exceeded" error.

Google client-side location

I will start with the code in question:
<script type="text/javascript">
if(google.loader.ClientLocation){
visitor_city = google.loader.ClientLocation.address.city;
document.getElementById('location').value = visitor_city;
}
</script>
In my case this code fails to produce the correct city at times. It some cases, it posts the correct city once, other times it posts the wrong city, and finally posts nothing at all. I need the city location to be correct in order to control access to my site so I can build a local following. The code was based on the information from this site.
I would like to continue using Google Location services and I am wondering whether anyone else has the same issue. If so, did you come up with a better solution that worked much better?
Edit: I am located in Ottawa, Ontario, Canada, so internet access or geolocation should not be an issue as I am not out in the boonies.

Javascript GeoLocation Caching

I'm using the following to successfully capture user's location (mobile browser):
<script>
if ( navigator.geolocation ) {
navigator.geolocation.getCurrentPosition(handlePosition);
}
function handlePosition(pos) {
//this passes lat/long to additional code
}
</script>
This works, but often times the browser will seemingly cache the location data. The page that calls this geolocation code shows information relative to the user's location, so what happens is the user can move (change location), the page is reloaded, but the previous location data is used (showing incorrect data). Sometimes the page will have to be refreshed once or even twice for the page to use new location data.
Does anyone know of any means to force the code to get and use "up to date" location data each time script is executed?
FWIW, I'm experiencing problem in iOS Safari (6.1). Have not been able to test in Android yet.
Thanks for reading and for any help.
Edit: As Oleksiy has written in his answer, the Geolocation API now supports this. You can add {maximumAge: 0} as the third option parameter of getCurrentPosition. There is also a timeout and a high accuracy option available in the PositionOptions as noted in the specification.
Your navigator call would change to the following:
navigator.geolocation.getCurrentPosition(
handlePosition,
(error)=>{},
{maximumAge:0}
);
No can't be done. You don't have any control over the browser geolocation other than the code in your example. The html5 geo location api is very, very limited and that is a pain. I also had a question whether I could ask it if permission for the domain had already been granted and the answer was also no.
The problem is that the api is implemented in the browser itself and that are just no endpoints for these kind of functions.
What you could do is make an array in js to store previous locations and before you update your view test against that array to see if you got a stale location.
You do have this ability now.
getCurrentPosition takes three parameters: success, failure and options
Try this:
<script>
if ( navigator.geolocation ) {
navigator.geolocation.getCurrentPosition(handlePosition, (error)=>{}, {maximumAge:0});
}
function handlePosition(pos) {
//this passes lat/long to additional code
}
</script>

Categories

Resources