Setting up the location source
You can use the QGeoPositionInfoSource class to find the location of a device and get updates about its position. Before you begin, read about the prerequisites for using location classes in your app. You can see these techniques in the location diagnostics app on the Sample apps page.
To get location fixes in your app, you must initialize a position source. Here's a code sample that initializes the default source for the location of a device:
// Set up the position info source. QGeoPositionInfoSource *src = QGeoPositionInfoSource::createDefaultSource(this);
You can also use the QNmeaPositionInfoSource class to initialize a NMEA data source, which can be either live positional data or previously recorded NMEA data. QNmeaPositionInfoSource inherits functions and signals from QGeoPositionInfoSource.
When the position source is initialized, you can choose a positioning method to specify how to find the location of the device. If you don't set a preferred positioning method, your app automatically uses the default positioning method, AllPositioningMethods, to get a location. There are three basic positioning methods that try multiple positioning methods simultaneously and return the first location fix found:
- All positioning methods: Tries all Global Navigation Satellite System (GNSS) and network methods at the same time, returning the first location found. As soon as a GNSS-based fix is available, it's preferred over any other fixes.
- Satellite positioning methods: Only tries Global Navigation Satellite System (GNSS) methods.
- Non-satellite positioning methods: Tries Wi-Fi and cell tower positioning methods. If a Wi-Fi fix is available, it's preferred over any other fixes.
You can explicitly specify which positioning method that you want to use to find the location of a device. For example, a turn-by-turn navigation app might want to only use a GPS positioning method. You can also set specific settings to allow your app to only use GPS, Wi-Fi, or cellular positioning methods. You can also specify carrier-assisted positioning methods. Carrier-assisted positioning methods allow you to specify exactly which assistance server to use to find the device's location.
You can read more about how to use each positioning method in the following sections. To learn more about restricting your positioning method, visit Using specific positioning methods.
Using all positioning methods
The default positioning method, AllPositioningMethods, tries to find the location of a device by using all Global Navigation Satellite System (GNSS) and network providers at the same time, although GNSS is tried in the background and returns a location when a GPS fix is available. The location that's returned to your app is the value that's found first. If two sources return a location at the same time, the GPS fix is returned to your app. If a Wi-Fi location is returned first because the device is in a building, this location fix will be returned to your app immediately; however, if the user walks out of the building, a GPS position is found and this location is returned to your app without requiring any changes to your code. This positioning method is recommended for most apps.
To use the default positioning source, set the preferred positioning method to AllPositioningMethods.
Using satellite positioning methods
If you want to only use satellite-based positioning methods to get a location in your app, you can set the preferred method to SatellitePositioningMethods. This method tries both autonomous GPS and mobile station-based positioning methods simultaneously and returns the location that is found first. If a mobile station-based position is available, it's preferred over an autonomous fix. Mobile station-based positioning is a carrier-based method. For more information about carrier-based methods, see Carrier-assisted positioning. If there are no available base stations to be used to find the position of a device, then this methodology falls back to autonomous positioning.
To only use satellite-based positioning methods, set the preferred positioning method to SatellitePositioningMethods.
Using non-satellite positioning methods
If you want to only use network-based positioning methods, you can set the preferred method to NonSatellitePositioningMethods. This method tries Wi-Fi and cell tower positioning methods simultaneously. If there is a Wi-Fi connection available, this method is used to find the device's position. If a Wi-Fi connection isn't available, cell tower positioning is used to find the device's position. A Wi-Fi fix is preferred over a cell tower-based fix.
To use non-satellite positioning methods, set the preferred positioning method to NonSatellitePositioningMethods.
Last modified: 2014-05-14