开发者

how to get user's position in Google maps API?

开发者 https://www.devze.com 2023-01-23 10:35 出处:网络
I noticed that the google m开发者_StackOverflow社区aps api has a option \"sensor\".\"sensor\" in GMaps API is not an option, it is an information parameter. Here is what docs say:

I noticed that the google m开发者_StackOverflow社区aps api has a option "sensor".


"sensor" in GMaps API is not an option, it is an information parameter. Here is what docs say:

Use of the Google Maps API(s) requires that you indicate whether your application is using a sensor (such as a GPS locator) to determine the user's location in any Maps API library or service requests.

Google Maps doesn't know anything of your users, so it cannot provide their location. It is the other way around: you can determine user's location and give it to Google Maps to place a location pointer and so on.

You have several options to get user's location in JS:

  • W3C (HTML 5) Geolocation API (needs browser support);
  • Google Gears API (needs Google Gears installed);
  • gelocation by IP, for instance, MaxMind and HostIP (the least accurate, but works most of the time).

If you're developing for a sensor-equipped device (like a mobile phone with GPS reciever) there should be some vendor-specific ways to get the location directly from the device.


The approach to "get user's position in Google maps" is dependent on which devices you are targeting.

The HTML 5 Approach is meant for Androids, iPhones and other mobile platforms that have support for HTML5. In this case the browser is able to obtain the co-ordinates directly from the GPS without you having to write the code in the native language.

However if you are developing on say QT to target Symbians and the like, you'll need to write the code in the native language (C++ in my case) to acquire the co-ordinates from your GPS and post it to the server to update the user's current location. You'll find details about retrieving co-ordinates using Qt Mobility API here.


Another way to get the user's position is to include:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

in your page.

This creates a javascript object "google.loader.ClientLocation" which contains the user's latitude, longitude and address.

Example:

google.loader.ClientLocation = {
    "latitude": 51.817,
    "longitude": 19.3,
    "address": {
        "city": "Aleksandrow Lodzki",
        "region": "Lodz",
        "country": "Poland",
        "country_code": "PL"
    }
};


For google maps API v3 it is described here: https://developers.google.com/maps/documentation/javascript/basics


The sensor parameter is no longer required. from here:

The Google Maps API previously required that you include the sensor parameter to indicate whether your application used a sensor to determine the user's location. This parameter is no longer required.

0

精彩评论

暂无评论...
验证码 换一张
取 消