简体   繁体   中英

How can I set my latitude and longitude for debugging the Geolocation API with Google Chrome?

For my development system I'd like to be able to set the Geolocation (Lat, Long) in Chrome so that when I'm doing the testing the browser thinks I'm at location X when I really might be at location Y.

Does anyone know how to force Google Chrome to use a Lat and Long that I provide as the location?

In Chrome today (version 42), open Developer Tools, click the "Toggle Device Icon", then in the "Emulation" drawer, chose "Sensors". There, you can Emulate geolocation coordinates and even "Emulate position unknown".

在此处输入图片说明

If you're talking about the Geolocation API, you can override the function:

navigator.geolocation.getCurrentPosition = function(success, failure) { 
    success({ coords: { 
        latitude: 30, 
        longitude: -105,

    }, timestamp: Date.now() }); 
} 

So when a library calls into the navigator.geolocation.getCurrentPosition function the coordinates you specify will be returned.

A little late on the answer, but in Chrome you can open the Developer Tools (F12 or Ctrl + Shift + I). In the lower right hand open the 'Settings' gear and switch to the 'Overrides' tab. There is a checkbox labeled 'Override Geolocation'. Check this box and enter in what ever geolocation that you want the site to think you are at.

In case I haven't explained it clearly enough, here is a good article that goes over it with screenshots and what not: http://www.labnol.org/internet/geo-location/27878/

您现在可以使用手动地理定位chrome 扩展来手动设置您的位置。

If you are looking for a way to do this in an automated fashion (eg Selenium in Python), this answer from another question ( Setting sensors (location) in headless Chrome ) works:

# Override selenium geolocation
location_key = "Emulation.setGeolocationOverride"
location_details = {
        "latitude": 34.0522342,
        "longitude": -118.2436849,
        "accuracy": 100
}
driver.execute_cdp_cmd(location_key, location_details, )

I am unaware of a way to do it in Chrome but you can do it in Firefox; this blog post will tell you how.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM