0

我想打印出由地理编码 javascript 计算的坐标(使用 Google api V3 制作),我该怎么做?

然后,我如何将此值传递给 php 文件中的两个变量($Lat 和 $Long),这些变量生成在 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?谢谢。

4

1 回答 1

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 一些事情,您需要加载一个新页面并使用标头、GET 或 POST 变量将数据从 JavaScript 传递到 PHP。您可以在浏览器中以正常方式加载此页面,并且您的用户会看到重定向,或者您可以使用 XMLHTTPRequest 在后台加载它。

于 2011-06-27T13:41:26.063 回答