开发者

Google Maps API - setting php variable as javascript variable

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.开发者_StackOverflow中文版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! :)


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.


"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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜