javascript - geolocation allow location exception causes site to fail going to the success function -


so geolocation code

<!-- <xml version="1.0" encoding="utf-8"> --> <!doctype html public "-//wapforum//dtd xhtml mobile 1.0//en" "http://www.wapforum.org/dtd/xhtml-mobile10.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>geolocation api demo</title> <meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" name="viewport"/> <script> function successhandler(location) {     var message = document.getelementbyid("message"), html = [];     html.push("<img width='400' height='400' src='http://maps.google.com/maps/api/staticmap?center=", location.coords.latitude, ",", location.coords.longitude, "&markers=size:small|color:blue|", location.coords.latitude, ",", location.coords.longitude, "&zoom=14&size=256x256&sensor=false' />");     html.push("<p>longitude: ", location.coords.longitude, "</p>");     html.push("<p>latitude: ", location.coords.latitude, "</p>");     html.push("<p>accuracy: ", location.coords.accuracy, " meters</p>");     message.innerhtml = html.join("");      var javascriptlongitude = location.coords.longitude;     var javascriptlatitude = location.coords.latitude;     var javascriptaccuracy = location.coords.accuracy;     window.location.href = "myphpfile.php?longitude=" + javascriptlongitude + "&" + "latitude=" + javascriptlatitude + "&" + "accuracy=" + javascriptaccuracy; } function errorhandler(error) {     alert('attempt location failed: ' + error.message); } navigator.geolocation.getcurrentposition(successhandler, errorhandler);  </script> </head> <body> <a href="http://jquery.com/">jquery</a>     <script src="jquery.js"></script> <div id="message">location unknown</div> </body> </html> 

i set variables in these 4 lines

var javascriptlongitude = location.coords.longitude; var javascriptlatitude = location.coords.latitude; var javascriptaccuracy = location.coords.accuracy; window.location.href = "myphpfile.php?longitude=" + javascriptlongitude + "&" + "latitude=" + javascriptlatitude + "&" + "accuracy=" + javascriptaccuracy; 

and myphpfile.php code

<?php $longitude = $_get["longitude"]; $latitude = $_get["latitude"]; $accuracy = $_get["accuracy"]; $return = " ".php_eol;  $myfile = fopen("newfile.txt", "w") or die("unable open file!"); fwrite($myfile, $longitude); fwrite($myfile, $return); fwrite($myfile, $latitude); fwrite($myfile, $return); fwrite($myfile, $accuracy); fclose($myfile); ?> 

my main problem when open site, , asks if allow know location, , click allow, works once...then when close tab , open again, doesn't work , shows me location unknown shown in code, fix that, have click on that little location icon

and have click clear these settings future visits or go chrome://settings/contentexceptions#location , delete site's item there it's gonna work once again...and can't done phones guess...

my second problem, , it's not big deal, when writing txt file, "\n", "\r\n" , php_eol don't work, tried adding them $longitude = $_get["longitude"].php_eol; , $longitude = $_get["longitude"]."\n"; still no luck

it seemed jquery.js tag stopped dom element loading. sure check if browser has feature built in because many still don't.

`

<div id="message">loading...</div> 

`

function successhandler(location) {    var message = document.getelementbyid("message"),      html = [];    html.push("<img width='400' height='400' src='http://maps.google.com/maps/api/staticmap?center=", location.coords.latitude, ",", location.coords.longitude, "&markers=size:small|color:blue|", location.coords.latitude, ",", location.coords.longitude, "&zoom=14&size=256x256&sensor=false' />");    html.push("<p>longitude: ", location.coords.longitude, "</p>");    html.push("<p>latitude: ", location.coords.latitude, "</p>");    html.push("<p>accuracy: ", location.coords.accuracy, " meters</p>");    message.innerhtml = html.join("");     //uncomment redirect.    //window.location.href = "myphpfile.php?longitude=" + location.coords.longitude + "&" + "latitude=" + location.coords.latitude + "&" + "accuracy=" +  location.coords.accuracy;  }   function errorhandler(error) {    alert('attempt location failed: ' + error.message);  }  if (navigator.geolocation) {    navigator.geolocation.getcurrentposition(successhandler, errorhandler);  } else {    alert("please download modern browser.");  } 

`

https://jsfiddle.net/iain17/ratzv34t/


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -