Javascript 从地理编码脚本打印坐标

Javascript 从地理编码脚本打印坐标,javascript,google-maps,geocoding,w3c,google-gears,Javascript,Google Maps,Geocoding,W3c,Google Gears,我想打印出由地理编码javascript(用Google api V3制作)计算的坐标,我该怎么做 然后,我如何将这些值传递给php文件中的两个变量($Lat和$Long),这两个变量在Api V2中生成一个google地图 谢谢 这是我的javascript代码: <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=HERE MY API KE

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

然后,我如何将这些值传递给php文件中的两个变量($Lat和$Long),这两个变量在Api V2中生成一个google地图

谢谢

这是我的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>

函数getLocale(){
if(导航器){
if(navigator.userLanguage){
返回navigator.userLanguage.toLowerCase();
}
else if(navigator.language){
返回navigator.language.toLowerCase();
}
else if(navigator.browserLanguage){
返回navigator.browserLanguage.toLowerCase();
}
else if(navigator.systemLanguage){
返回navigator.systemLanguage.toLowerCase();
}
}
返回“未知”;
}
var locales=新对象();
地区[“英国”]={lat:54.559322587438636,液化天然气:-4.1748046875,地点:“英国”};
地区[“美国”]={lat:38.41055825094609,液化天然气:-100.37109375,地点:“美国”};
//待办事项-更多地区
功能显示地图(拉长、缩放){
变量选项={
缩放:缩放,
中心:拉特朗,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map”),选项);
var marker=new google.maps.marker({
职位:拉特朗,
地图:地图,
标题:“你的位置”
});
}
函数TryGoogleLoader(){
if(google.loader.ClientLocation!=null){
var address=google.loader.ClientLocation.address;
var yourLocation=address.city+,“+address.region+”,“+address.country;
document.getElementById(“location”).innerHTML=“您的位置(使用Google loader)是”+yourLocation;
var latLong=new google.maps.LatLng(google.loader.ClientLocation.latitude,
google.loader.ClientLocation.longitude);
展示地图(拉特朗,12);
}
否则{
//将区域设置映射到位置
var locale=getLocale();
if(locales[locale]!=null){
var latLong=new google.maps.LatLng(locales[locale].lat,locales[locale].lng);
document.getElementById(“位置”).innerHTML=
“根据您的区域设置猜测您的位置-”+区域设置[locale]。位置;
展示地图(拉特朗,5);
}
否则{
document.getElementById(“location”).innerHTML=“找不到您的位置-区域设置为”+区域设置;
}
}
}
函数TryGoogleGears(){
if(google.gears){
//试试谷歌齿轮地理定位
var geo=google.gears.factory.create('beta.geolocation');
geo.getCurrentPosition(函数(位置){
var latLong=新的google.maps.LatLng(位置.纬度,位置.经度);
document.getElementById(“location”).innerHTML=“通过Google Gears找到位置”;
展示地图(拉特朗,15);
},函数(){
TryGoogleLoader();
});
}
其他的
TryGoogleLoader();
}
window.onload=函数(){
//尝试W3C标准方法
var-geoTimeout=10000;
var timeOuthandler=setTimeout(“TryGoogleGears()”,geotimout);
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
clearTimeout(timeOuthandler);
var latLong=new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
document.getElementById(“location”).innerHTML=“通过W3C标准找到位置”;
展示地图(拉特朗,15);
},函数(){
//出现问题,请尝试谷歌齿轮
clearTimeout(timeOuthandler);
TryGoogleGears();
},{timeout:geoTimeout});
}
其他的
TryGoogleGears();
}
如何使用警报代码打印“var latLong”的值? 如何将该值传递给一组php变量,如$lat和$long?
谢谢。

对于W3C函数,您可以在成功后执行此操作

document.getElementById("location").innerHTML = position.coords.latitude +", "+position.coords.longitude;
document.getElementById("location").innerHTML = position.latitude +", "+position.longitude;
在TryGoogleGears功能中,您可以在成功时执行此操作

document.getElementById("location").innerHTML = position.coords.latitude +", "+position.coords.longitude;
document.getElementById("location").innerHTML = position.latitude +", "+position.longitude;
为了将LatLong对传递回PHP,您需要使用a或类似的东西。这是因为PHP是一个服务器端脚本,在客户端运行JavaScript之前执行。因此,要告诉PHP一些事情,您需要加载一个新页面,并使用头、GET或POST变量将数据从JavaScript传递到PHP。您可以在浏览器中以正常方式加载此页面,用户会看到重定向,也可以使用XMLHTTPRequest在后台加载此页面