1

I am using the Google Maps API and the W3C Standard to get a user's geolocation. I have it set up as basics - using the code from [Google's documentation][1].

Now I'm integrating this into my website and need it imputed into my mysql database. I want to set a PHP variable as a JavaScript variable (basically setting a PHP variable as the latitude and longitude). But I'm having no success of doing this.

EDIT:

Basically I want to make $phpvar = $jsvar. Making a php variable = my javascript variable.

Here's the code:

var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag =  new Boolean();

function initialize() {
  var myOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  // Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  // Try Google Gears Geolocation
  } else if (google.gears) {
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeoLocation(browserSupportFlag);
    });
  // Browser doesn't support Geolocation
  } else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }

  function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = newyork;
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = siberia;
    }
    map.setCenter(initialLocation);

} }

Any help would be great! Please be specific with me because I'm new to this Google Map API.

Thanks! :)

3
  • if you can provide some code you are using, it will be easier to explain with the help of a code.. The basic syntax goes as I said in my answer. Commented Dec 29, 2010 at 19:32
  • Stoic, Please see my edit along with the code I'm using. Commented Dec 29, 2010 at 19:39
  • 1
    seems like you are rather wanting to store a Javascript variable into your SQL database. If thats the case, you will need to use AJAX (which is easier than DOM hacking, IMHO) Commented Dec 29, 2010 at 19:45

3 Answers 3

1

You can store a PHP variable as a Javascript variable like this:

<script type="text/javascript">
    var geoloc = '<?php echo $geoloc; ?>';
</script>

You can then use this variable as required. If you can give an example code, I would try to let you know more accurately.

Sign up to request clarification or add additional context in comments.

2 Comments

This is vulnerable to injection - what if $geoloc contains a single quote? Now you've broken this javascript block. Just like mysql_real_escape_string() for SQL work, you need to run things through json_encode() before inserting into javascript.
Hey Marc.. that was an example statement.. and since he is using latitudes and longitudes, he wont be actually requiring a json_encode here to increase complexity for himself. Moreover, in general, defintely the example line will need to be changed as required.
1

"want to set a PHP variable as a Javascript variable". This isn't quite clear?

If you want to go PHP->Javascript, then it's simple:

var myVar = <?php echo json_encode($some_php_var) ?>;

For Javascript->PHP, it's a bit more complicated. You'd have to do some DOM hacking to insert the value into a form value/submit form, or do some AJAX work.

Comments

0

This is how I currently set the php variables for Lat and Lng and then call them with Javascript:

function initMap() {
  var geoLat = parseFloat("<?php echo h($location['lat']); ?>");
  var geoLng = parseFloat("<?php echo h($location['lng']); ?>");
  var theLocation = {lat: geoLat, lng: geoLng};

var map = new google.maps.Map(
    document.getElementById('map'), {
    zoom: 16,
    center: theLocation
});

I'm using the parseFloat() function parseFloat

This helped solve the Lat/Lng Object Literal error messages and markup / lint errors in VS Code that some may be experiencing. I hope this helps you and others.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.