AMap.Geolocation
The positioning service plugin integrates various methods such as browser positioning, high-precision IP positioning, and Android positioning SDK assisted positioning, providing functions such as obtaining the current accurate location, obtaining current city information, and continuous positioning (browser positioning). Users can obtain the success or failure and results of the positioning in two ways: one is to pass a callback function when getCurrentPosition is called to handle the positioning result, and the other is to obtain the positioning result through event listening.
Construct a positioning service object, and set the properties of the service object through GeolocationOptions
new AMap.Geolocation(options: GeolocationOptions)
Parameter
opts(GeolocationOptions): Positioning parameters
Attribute | Type | Description |
position | string | The hover position, default is "RB", which is the bottom right corner |
offset | [number, number] | The pixel distance of the thumbnail from the hover position, such as [2,2] |
borderColor | string | The border color value of the button, same as CSS, such as 'silver' |
borderRadius | string | The border-radius value of the button, same as CSS, such as '5px' |
buttonSize | string | The pixel size of the arrow button, same as CSS, such as '12px' |
convert | boolean | Whether to convert the positioning result to Amap coordinates |
enableHighAccuracy | boolean | Whether to attempt to obtain higher accuracy during native browser positioning, which may affect positioning efficiency, default is false |
timeout | number | Timeout time for location, in milliseconds |
maximumAge | number | Cache time for browser native location, in milliseconds |
showButton | boolean | Whether to display the location button, default is true |
showCircle | boolean | Whether to display the location accuracy circle, default is true |
showMarker | boolean | Whether to display the location point, default is true |
markerOptions | MarkerOptions | Style of the positioning point |
circleOptions | CircleOptions | Style of the positioning circle |
panToLocation | boolean | Whether to automatically move to the response position after successful positioning |
zoomToAccuracy | boolean | Whether to automatically adjust the level after successful positioning |
GeoLocationFirst | boolean | Prefer H5 positioning, default is true for mobile and false for PC |
noIpLocate | number | Whether to disable IP precise positioning, the default is 0, 0: use all 1: do not use on mobile phones 2: do not use on PCs 3: do not use all |
noGeoLocation | number | Whether to disable the browser's native positioning, the default is 0, 0: use all 1: do not use on mobile phones 2: do not use on PCs 3: do not use all |
useNative | boolean | Whether to integrate with the capabilities of the Amap Positioning SDK, it is necessary to use the Android version of the Amap Positioning SDK at the same time, otherwise it will be invalid |
getCityWhenFail | boolean | Whether to return basic city positioning information after positioning failure |
needAddress | boolean | Whether to perform reverse geocoding operation on the positioning result |
extensions | string | Whether detailed reverse geocoding information is needed, default is 'base' to return only basic information, optional 'all' |
Demo
AMap.plugin("AMap.Geolocation", function () {
var geolocation = new AMap.Geolocation({
timeout: 10000, // Set the location timeout, default: infinity
});
geolocation.getCurrentPosition(function (status, result) {
//status: complete indicates a successful query, error represents a query error
//When the query is successful, the result is the relevant location information
});
});
Method
getCurrentPosition(callback)
Get the user's precise location, with a chance of failure
Parameter: callback (GeolocationCallBack) The location callback function will be called whether it succeeds or fails
Demo:
geolocation.getCurrentPosition(function (status, result) {
//status: complete indicates a successful query, error represents a query error
//When the query is successful, the result is the relevant location information
});
getCityInfo(callback)
Retrieve the user's city information based on their IP address
Parameter: callback (GeolocationCallBack)
Demo:
geolocation.getCityInfo(function (status, result) {
//status: complete indicates a successful query, error represents a query error
//When the query is successful, the result is the relevant location information
});