推荐网站 https://html5demos.com/geo/ 

 我们有时候可能希望首先获得用户的地理位置,然后根据不同的地理位置(更具针对性地)推送不同的信息等等。

  下面这段代码就可以在你有jQuery的条件下alert()你所在的区域:

$.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js", function () {
var province = remote_ip_info["province"] + '省';
alert(province);
});

  由于依赖的是jQuery,所以将$换成jQuery也可以达到相同的效果。

jQuery.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js", function () {
var province = remote_ip_info["province"] + '省';
alert(province);
});

  我们可以看到调用了jQuery的一个方法,其中第一个参数是一个url,然后第二个参数时一个回调函数,这个回调函数中remote_ip_info["province"]即可得到用户所在的省份。

  既然province时remote_ip_info对象的其中一个属性,那么remote_ip_info一定还有其他的属性,我们通过下面的代码来观察remote_ip_info这个对象究竟是什么:

$.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js", function () {
console.log(remote_ip_info); });

  在我的控制台中可以得到下面的信息:

  即remote_ip_info对象中有city、country、desc 、disctrict、 isp、province、 ret、 start、 type等属性。 其中我们最为需要的恐怕是 中国-陕西-西安 了。

  那为什么是这样的结果呢? 我们将第一个参数(url)输入到浏览器的地址栏中,可以看到,得到如下的代码:

var remote_ip_info = {"ret":,"start":-,"end":-,"country":"\u4e2d\u56fd","province":"\u9655\u897f","city":"\u897f\u5b89","district":"","isp":"","type":"","desc":""};

  我们可以看到他就是定义了这样的一个对象,我们得到的就是对应格式的对象。

使用谷歌地图。

<html>
<meta name="viewport" content="width=620" />
<title>geolocation</title>
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<article>
<p>Finding your location: <span id="status">checking...</span></p>
</article>
<script>
function success(position) {
var s = document.querySelector('#status'); if (s.className == 'success') {
// not sure why we're hitting this twice in FF, I think it's to do with a cached result coming back
return;
} s.innerHTML = "found you!";
s.className = 'success'; var mapcanvas = document.createElement('div');
mapcanvas.id = 'mapcanvas';
mapcanvas.style.height = '400px';
mapcanvas.style.width = '560px'; document.querySelector('article').appendChild(mapcanvas); var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeControl: false,
// mapTypeControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions); var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"You are here! (at least within a "+position.coords.accuracy+" meter radius)"
});
} function error(msg) {
var s = document.querySelector('#status');
s.innerHTML = typeof msg == 'string' ? msg : "failed";
s.className = 'fail'; // console.log(arguments);
} if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
error('not supported');
} </script>
</html>

  

如何获取用户的地理位置? && html5 地理位置的更多相关文章

  1. 关于html5获取用户地理位置

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  2. 如何获取用户的地理位置-浏览器地理位置(Geolocation)API 简介

    如何获取用户的地理位置-浏览器地理位置(Geolocation)API 简介 一.总结 一句话总结:Geolocation API(地理位置应用程序接口)提供了一个可以准确知道浏览器用户当前位置的方法 ...

  3. web网站如何获取用户的地理位置

    web网站如何获取用户的地理位置 一.总结 一句话总结:通过gps知道用户的经度和纬度,然后通过经度和纬度在在地图(google或者百度)上面显示位置. 1.html5如何通过gps知道用户的经度和纬 ...

  4. 使用navigator.geolocation来获取用户的地理位置信息

    使用navigator.geolocation来获取用户的地理位置信息 W3C 中新添加了一个名为 Geolocation的 API 规范,Geoloaction API的作用就是通过浏览器获取用户的 ...

  5. 根据ip获取用户地理位置

    各大网站都提供根据ip获取用户地理位置信息,这里以新浪的接口为例子 接口地址为:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js ...

  6. PHP获取用户真实 IP , 淘宝IP接口获得ip地理位置(转)

    <?php /** * 获取用户真实 IP */ function getIP() { static $realip; if (isset($_SERVER)){ if (isset($_SER ...

  7. 手机端获取用户详细地理位置(高德地图API)

    项目开发需要获取用户详细的地理位置信息,使用了高德地图API接口 1,注册高德地图开发者账号获取开发者Key 2,页面调用 <script type="text/javascript& ...

  8. 使用纯真IP库获取用户端地理位置信息

    引言 在一些电商类或者引流类的网站中经常会有获取用户地理位置信息的需求,下面我分享一个用纯真IP库获取用户地理位置信息的方案. 正文 第一步:本文的方案是基于纯真IP库的,所以首先要去下载最新的纯真I ...

  9. js获取用户实时地理位置

    js获取用户实时地理位置 if(navigator.geolocation) { var id = navigator.geolocation.watchPosition(function(posit ...

  10. 百度地图JavaScript API获取用户当前经纬度和详细地理位置,反之通过详细地理位置获取当前经纬度

    前言: 前端时间刚好使用了百度地图的js api定位获取用户当前经纬度并获取当前详细位置和通过当前用户详细地理位置换取用户当前经纬度坐标的功能,为了方便下次找起来方便一些自己在这里记录一下,希望也能够 ...

随机推荐

  1. Creating Custom UITableViewCells with NIB files

    Maksim Pecherskiy 13 November 2012 Well this sucks. Apparently these days you can only use the Inter ...

  2. 字节序(Endian),大端(Big-Endian),小端(Little-Endian)

    http://www.cppblog.com/tx7do/archive/2009/01/06/71276.html 在各种计算机体系结构中,对于字节.字等的存储机制有所不同,因而引发了计算机通信领域 ...

  3. Gym - 100989H (贪心)

    After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...

  4. Android-看操作系统短信应用源码-隐式意图激活短信界面

    选择模拟器Unknown Google Nexus,在选择system_process(系统进程) 操作模拟器的,操作系统短信应用,让操作系统短信打印日志,来查看: 然后就找到来,操作系统短信应用打印 ...

  5. Android-自定义IntentSession来传递数据

    在上一篇博客中介绍到,Android-Intent意图传递数据,能够传递基本数据类型系列,能够传递对象(需要序列化),等操作: 但是如果要传递 List<T>,这种类型的数据,就不能通过I ...

  6. 我眼中的SAML (Security Assertion Markup Language)

    提到SAML (Security Assertion Markup Language), 很多人都会联想到单点登录SSO.那么Saml到底是什么,它跟sso到底有什么联系?这里给大家分享一下我在读完了 ...

  7. Centos 固定ip

    vim /etc/sysconfig/network-scripts/ifcfg-eth0 BOOTPROTO="static" ONBOOT=yes IPADDR=192.168 ...

  8. Spring Boot - Spring Cache

    缓存 服务器自身(内存)的缓存 利用java程序中的变量 简单 集群环境中多个实例无法共享同步 缓存服务器(一般支持集群.分布式) Redis Memcached Spring中使用注解使用缓存 启动 ...

  9. Android Post方式发送信息和获取URL中的图片

    需要Internet权限,AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET& ...

  10. orcal exists

    Oracle使用了一个复杂的自平衡B-tree结构.通常,通过索引查询数据比全表扫描要快.当 Oracle找出执行查询和Update语句的最好路径时,Oracle优化器将使用索引.同样在联结多个表时使 ...