从地理编码脚本中打印出坐标

时间:2011-06-24 13:26:28

标签: javascript google-maps geocoding w3c google-gears

我想打印出地理编码javascript(与Google api V3合作)计算的坐标,我该怎么办?

然后,我怎样才能将这个值传递给两个变量($ Lat和$ Long),这两个变量位于一个php文件中,该文件生成一个以Api V2为主的谷歌地图?

感谢。

这是我的javascript代码:

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

如何使用警报代码打印“var latLong”的值? 我如何将此值传递给一组PHP变量,如$ lat和$ long? 感谢。

1 个答案:

答案 0 :(得分:0)

在W3C功能上,您可以成功完成此操作

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

在TryGoogleGears功能上,您可以成功完成此操作

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

为了将LatLong对传递回PHP,您需要使用XMLHTTPRequest或类似的东西。这是因为PHP是在JavaScript之前执行的服务器端脚本,它在客户端上运行。因此,要告诉PHP需要加载新页面并使用header,GET或POST变量将数据从JavaScript传递给PHP。您可以在浏览器中以正常方式加载此页面,用户可以看到重定向,也可以使用XMLHTTPRequest在后台加载。