开发者

Print out coordinates from a geocoding script

I want to print out the coordinates calculated by a geocoding javascript ( maded with Google api V3 ), how can i do that??

and then, how can i pass this values to two variables ($Lat and $Long) that are in a php file that generate a google map maded in Api V2 ??

thanks.

this is my javascript code:

    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=HERE MY API KEY" type="text/javascript"></script>


<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script>

<script type="text/javascript">

    function getLocale(){
      if ( navigator ) {
        if ( navigator.userLanguage ) {
            return navigator.userLanguage.toLowerCase();
        }
        else if ( navigator.language ) {
            return navigator.language.toLowerCase();
        }
        else if ( navigator.browserLanguage ) {
            return navigator.browserLanguage.toLowerCase();
        }
        else if ( navigator.systemLanguage ) {
            return navigator.systemLanguage.toLowerCase();
        }
      }
      return "unknown";
    }

    var locales = new Object();
    locales["en-gb"] = {lat:54.559322587438636, lng:-4.1748046875, location:"United Kingdom"};
    locales["en-us"] = {lat:38.41055825094609, lng:-100.37109375, location:"USA"};
    // TODO - more locales

    function showMap(latLong, zoom){
      var options = {
        zoom: zoom,
        center: latLong,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map"), options);
      var marker = new google.maps.Marker({
          position: latLong,
          map: map,
          title:"Your location"
      });
    }

    function TryGoogleLoader(){
      if (google.loader.ClientLocation != null) {
        var address = google.loader.ClientLocation.address;
        var yourLocation = address.city + ", " + address.region + ", " + address.country;

        document.getElementById("location").innerHTML = "Your location (using Google loader) is " + yourLocation;
        var latLong = new google.maps.LatLng(google.loader.ClientLocation.latitude,
          google.loader.ClientLocation.longitude);
        showMap(latLong, 12);开发者_开发百科
        }
        else {
        // map locale to location
        var locale = getLocale();
        if (locales[locale] != null) {
          var latLong = new google.maps.LatLng(locales[locale].lat, locales[locale].lng);
          document.getElementById("location").innerHTML =
            "Guessing your location based on your locale - " + locales[locale].location;
          showMap(latLong, 5);
        }
        else {
          document.getElementById("location").innerHTML = "Your location can not be found - locale is " + locale;
        }
      }
    }

    function TryGoogleGears(){
      if (google.gears) {
        // Try Google Gears Geolocation
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(function(position) {
          var latLong = new google.maps.LatLng(position.latitude, position.longitude);
          document.getElementById("location").innerHTML = "Found location via Google Gears";
          showMap(latLong, 15);
        }, function() {
          TryGoogleLoader();
        });
      }
      else
        TryGoogleLoader();
    }

    window.onload = function() {

      // try W3C standard approach
      var geoTimeout = 10000;
      var timeOuthandler = setTimeout("TryGoogleGears()", geoTimeout);
      if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          clearTimeout(timeOuthandler);
          var latLong = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
          document.getElementById("location").innerHTML = "Found location via W3C standard";
          showMap(latLong, 15);
        }, function() {
          // something went wrong, try Google Gears
          clearTimeout(timeOuthandler);
          TryGoogleGears();
        }, {timeout:geoTimeout});
      }
      else
        TryGoogleGears();
    }

</script>

how can i print with an alert code the value of the "var latLong"? how can i pass this value to a set of php variables like $lat and $long? thanks.


On the W3C function you could do this on success

document.getElementById("location").innerHTML = position.coords.latitude +", "+position.coords.longitude;

On the TryGoogleGears function you could do this on success

document.getElementById("location").innerHTML = position.latitude +", "+position.longitude;

In order to pass the LatLong pair back to PHP you will need to use a XMLHTTPRequest or something similar. This is because PHP is a server side script that executes before JavaScript does, which runs on the client. So to tell PHP something you need to either load a new page and pass the data from JavaScript to PHP with headers, GET, or POST variables. You can either load this page the normal way in the browser and your users see a redirect or you can load it in the background using XMLHTTPRequest.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜