basic class
Before developing with JS API, there are a few basic types you need to understand, including:
- latitude and longitudeAMap.LngLat
- pixelAMap.Pixel
- pixel size AMap.Size
- Latitude and longitude rectangle boundary AMap.Bounds
latitude and longitude AMap.LngLat
Latitude and longitude are a coordinate system that uses three-dimensional spherical space to describe a location on the Earth, with each latitude and longitude coordinate consisting of two components: longitude lng and latitude lat. In the JS API, latitude and longitude are used to represent the center point center of a map, the position position of a point marker, the center point center of a circle, the path path of a polyline and polygon, and all other places that describe the actual location。
Format
The valid range of latitude and longitude is [-180, 180], and the latitude is approximately [-85, 85].
There are two ways to write latitude and longitude supported by JS API. When using, longitude is in front and latitude is in the back. It is recommended to use the standard writing:
var position = new AMap.LngLat(77, 28);//Standard writing
var position = [77, 28]; //Abbreviation
var map = new AMap.Center('conatiner',{
center:position
})When using a latitude and longitude array, for example, to create a path of a polyline, write as follows:
var path = [new AMap.LngLat(77, 28), new AMap.LngLat(77, 29), new AMap.LngLat(78,28)] //Standard writing
var path = [ [77, 28], [77, 29], [78,28] ]; //Abbreviation
var polyline = new AMap.Polyline({
path : path,
})
map.add(polyline);The following writing style is currently not supported:
var position = '77, 28'
var position = ['77','28']
var path = [ '77,28', '78,28', '77,29']Calculate
Using the latitude and longitude type, you can perform some simple location calculations, such as the distance between points, the distance between a point and a line, and calculating another latitude and longitude based on the distance difference, etc.:
var lnglat1 = new AMap.LngLat(77,28);
var lnglat2 = new AMap.LngLat(78,28);
var distance = lnglat1.distance(lnglat2);//Calculate the actual distance (m) between lnglat1 and lnglat2
var lnglat3 = lnglat1.offset(100,50)//The latitude and longitude of the position 100m east and 50m north of lnglat1Pixel Point AMap.Pixel
A pixel point consists of two components: x and y, typically used to describe the container coordinates of a map, geographic pixel coordinates (planar pixel coordinates), point markers, and anchor points of information windows. The usage is as follows:
var offset = new AMap.Pixel(-16,-30);
var marker = new AMap.Marker({
offset:offset,
icon:'xxx.png',
});
map.add(marker);Pixel Dimensions AMap.Size
The pixel dimensions are composed of two components: width and height, typically used to describe objects of a certain size, such as the size of a map, the size of an icon, etc.
var mapSize = map.getSize();//Get the map size, which returns the pixel size of the map container
var width = mapSize.width;
var height = mapSize.height;
var marker = new AMap.Marker({
position: [77.19, 28.59],
icon: new AMap.Icon({
size: new AMap.Size(40, 50), //Size of the icon
image: "https://webapi.amap.com/theme/v1.3/images/newpc/way_btn2.png",
imageOffset: new AMap.Pixel(0, -60)
})
});
map.add(marker)Rectangular Latitude and Longitude Boundary AMap.Bounds
The rectangular latitude and longitude boundary is a range of latitude and longitude coordinates described by the southwest and northeast corners, representing the minimum and maximum values of the boundary respectively. Rectangular latitude and longitude boundaries are commonly used to describe: the current boundary of a map, the bounding rectangle boundary of an overlay, the coverage area of an image layer, etc. For example, constructing a new Bounds object to adjust the boundary range of a map:
var southWest = new AMap.LngLat(110,20);
var northEast = new AMap.LngLat(120,30);
var bounds = new AMap.Bounds(southWest, northEast);
map.setBounds(bounds);