【问题标题】:Geolocation with Javascript without any map [closed]没有任何地图的Javascript地理定位[关闭]
【发布时间】:2013-01-10 18:49:48
【问题描述】:

我正在尝试使用 javascript 进行地理定位。我一直在搜索,但我只能找到使用地图的示例。我只想在标签、文本框或任何纯文本小部件中显示用户的地址。

【问题讨论】:

  • 为什么不使用 html 5 的新地理定位功能?
  • 我开始认为显示地址而不是位置会更有帮助
  • 地址,据我所知,您将不得不使用外部服务。例如,您可以使用 GeoNames 从地理位置获取国家/地区。我曾经问过一个问题如何实现这一点:stackoverflow.com/questions/13370421/… 但是,它使用 php 而不是 javascript

标签: javascript geolocation


【解决方案1】:
<script src="http://j.maxmind.com/app/geoip.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" >
window.onload=getGeo;

function getGeo(){
    if (navigator && navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(geoOK, geoKO);
    } else {
        geoMaxmind();
    }
}

function geoOK(position) {
     showLatLong(position.coords.latitude, position.coords.longitude);
}
function geoMaxmind() {
     showLatLong(geoip_latitude(), geoip_longitude());
}

function geoKO(err) {
if (err.code == 1) {
error('El usuario ha denegado el permiso para obtener informacion de ubicacion.');
} else if (err.code == 2) {
error('Tu ubicacion no se puede determinar.');
} else if (err.code == 3) {
error('TimeOut.')
} else {
error('No sabemos que pasó pero ocurrio un error.');
}
}

function showLatLong(lat, longi) {
var geocoder = new google.maps.Geocoder();
var yourLocation = new google.maps.LatLng(lat, longi);
geocoder.geocode({ 'latLng': yourLocation },processGeocoder);

}
function processGeocoder(results, status){

if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
document.forms[0].dir.value=results[0].formatted_address;
document.getElementById("")
} else {
error('Google no retorno resultado alguno.');
}
} else {
error("Geocoding fallo debido a : " + status);
}
}
function error(msg) {
alert(msg);
}
</script>

<form align="left">
        <input type="text" name="dir" />
</form>

【讨论】:

    【解决方案2】:

    查看this example:

    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
    }
    
    function showPosition(position) {
        alert(position.coords.latitude + ", " + position.coords.longitude;
    }
    

    【讨论】:

      【解决方案3】:

      查看 HTML5 地理位置 API:http://diveintohtml5.info/geolocation.html

      很简单:

      navigator.geolocation.getCurrentPosition(function(location) {
          // This function is called if the location is found. Use location.coords.latitude & location.coords.longitude
      }, function() {
          // This function is called if the location is not found.
      });
      

      但并非所有浏览器都支持它。如果您要公开,我还建议使用 Modernizr 来检测支持:http://modernizr.com

      【讨论】:

        【解决方案4】:

        您可以使用geoplugin

        将以下内容添加到您的 &lt;head&gt; 标记中。

        <script  src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
        

        这将根据您的IP address 确定位置。

        在你的javascript中,你可以调用

        latitude = geoplugin_latitude();
        longitude = geoplugin_longitude();
        

        【讨论】:

          【解决方案5】:

          试试这个:

           <script src="http://www.merkwelt.com/people/stan/geo_js/js/geo.js?id=1" type="text/javascript" charset="utf-8"></script>
               <script type="text/javascript">
                   if (geo_position_js.init()) {
                       geo_position_js.getCurrentPosition(success_callback, error_callback, { enableHighAccuracy: true });
                   }
                   else {
                       alert("Functionality not available");
                   }
          
                   function success_callback(p) {
                       //alert('lat=' + p.coords.latitude.toFixed(2) + ';lon=' + p.coords.longitude.toFixed(2));
                       longitude = p.coords.longitude.toFixed(2);
                       latitude = p.coords.latitude.toFixed(2);
                   }
          
                   function error_callback(p) {
                       alert('error=' + p.code);
                   }
          </script>
          

          然后根据经纬度,可以使用google api获取位置: http://maps.googleapis.com/maps/api/geocode/json?latlng=33.84,35.52&sensor=false

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-12
            • 2013-10-13
            • 1970-01-01
            相关资源
            最近更新 更多