问题描述
想使用google.loader.ClientLocation与地图API一起获取访问者的位置,并在其上居中的地图。以下是工作的罚款,我(在Safari,Firefox和Chrome),但朋友我来测试它(在Safari和Firefox)只看到一个白色的盒子与谷歌标志。
Am trying to use google.loader.ClientLocation along with the maps api to get the location of the visitor and center a map on it. The following is working fine for me (on Safari, Firefox and Chrome), but a friend I got to test it (in Safari and Firefox) just sees a white box with the Google logo.
<script type="text/javascript" src="http://www.google.com/jsapi?key=MykeyHere"></script>
<script type="text/javascript">
google.load("maps", "2");
</script>
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("local_map"));
map.setMapType(G_PHYSICAL_MAP);
map.setCenter(new GLatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude), 15);
// map.setCenter(new GLatLng(54.5975, -5.920278), 15);
map.setUIToDefault();
}
</script>
任何想法可能是想错了?该网站是在(地图是靠近底部)
推荐答案
谷歌ClientLocation使用IP地址的地理位置。有几个IP地址的地理位置提供商那里(在右侧边栏的相关问题会给你几个的那些),他们给出不同的结果给不同的用户。谷歌ClientLocation将无法找到每一个人,它不会总是给那些为precise或某些竞争对手准确的答案。
Google ClientLocation uses IP address geolocation. There are several IP address geolocation providers out there (the related questions in the sidebar on the right will give you several of those) and they give different results to different users. Google ClientLocation won't be able to locate everyone and it won't always give answers that are as precise or as accurate as some competitors.
在,比如我使用谷歌ClientLocation和的和谷歌ClientLocation在家里没有找到我的权利,而IPInfoDB认为我在Sunnyvale,而不是伯克利分校。您可能会发现一个提供比其他(特别是如果你支付其数据库)更好,但我怀疑,最可靠的方法,如果你真的需要它,就是用一些,并希望至少有一个将有结果。你应该知道预先声明的IP地理定位并不总是对工作的所有的用户(那些背后的一个匿名代理,例如,将永远不会位于此方式)。
On my test page, for example, I use Google ClientLocation and IPInfoDB and Google ClientLocation doesn't find me at home right now, while IPInfoDB thinks I'm in Sunnyvale instead of Berkeley. You may find that one provider is better than the others (particularly if you pay for its database), but I suspect that the most robust method, if you really need it, is to use several and hope that at least one will have a result. You should know up front that IP geolocation will not always work for all users (those behind an anonymizing proxy, for example, will never be located this way).
同时,如果你想要一个更precise位置,您可以使用JavaScript API来要求浏览器为用户的确切位置。在支持的浏览器用户将收到一个小的弹出窗口,询问他们的许可,如果给定的,浏览器会再使用GPS(移动Safari浏览器在iPhone上,例如)或WiFi三角测量(火狐3.5+),以确定经纬度。你可以看到这是如何工作的,并将其与IP地理定位比较。并非所有的浏览器都支持,但越来越多的事情。
Also, if you want a more precise location, you can use a Javascript API to ask the browser for the user's exact location. On supported browsers the user will receive a small pop-up asking for their permission and, if given, the browser will then use GPS (Mobile Safari on the iPhone, for example) or WiFi triangulation (Firefox 3.5+) to determine latitude and longitude. You can see how this works on my test page, and compare it with IP geolocation. Not all browsers support the W3C Geolocation API, but a growing number do.
这篇关于获取用户的位置,google.loader.ClientLocation为我工作,但不是别人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!