1、概述
今天再一次用到了地图定位功能和地图显示功能,之前一直用的是高德地图定位,此次仍是用高德,记录一下以便之后再作时方便一些。由于业务须要的功能不是很全,能够参考https://www.2cto.com/kf/201504/396156.htmlhtml
高德地图申请成为开发者后的限制次数:android
Key平台类型git
服务api
我的开发者缓存
认证我的开发者网络
企业开发者并发
调用量ide
(次/日)ui
并发量this
(次/秒)
调用量
(次/日)
并发量
(次/秒)
调用量
(次/日)
并发量
(次/秒)
Web服务API
地理编码
6000
100
300000
200
3000000
1000
逆地理编码
6000
100
300000
200
3000000
1000
驾车路径规划
2000
50
30000
50
300000
200
公交路径规划
2000
50
30000
50
300000
200
步行路径规划
2000
50
30000
50
300000
200
骑行路径规划
2000
50
30000
50
300000
200
距离测量
2000
50
30000
50
300000
200
搜索
2000
50
30000
50
300000
200
输入提示
2000
50
30000
50
300000
200
行政区查询
2000
50
30000
50
300000
200
交通态势
2000
20
30000
50
300000
200
抓路服务
2000
50
30000
50
300000
200
云图搜索
2000
20
30000
50
300000
200
云图存储
2000
20
30000
50
300000
200
静态地图
100000
400
3000000
500
6000000
1000
IP定位
100000
100
300000
200
3000000
1000
坐标转换
100000
100
300000
200
3000000
1000
天气查询
100000
100
300000
200
3000000
1000
智能硬件定位
没法申请Key
没法申请Key
没法申请Key
没法申请Key
3000000
1000
二、图片效果:
四、首先是根据当前位置得到经纬度activity
(1)androidMainfest.xml中写入高德地图api的key值
android:name="com.amap.api.v2.apikey"
android:value="9cb6f84fb4821043af1bd1*******" />
(2)引入地图和定位的jar包(到网上下载最新的)
//地图显示功能
compile files('libs/AMap_2DMap_V2.9.0_20160525.jar')
//地图定位功能
compile files('libs/AMap_Location_V2.5.0_20160526.jar')
(3)Activity类实现定位监听
public class MainBuildingActivity extends MainActivity implements AMapLocationListener
(4)
private AMapLocationClient locationClient = null;
private AMapLocationClientOption locationOption = null;
locationClient = new AMapLocationClient(this.getApplicationContext());
locationOption = new AMapLocationClientOption();
// 设置定位模式为高精度模式
locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
// 设置定位监听
locationClient.setLocationListener(this);
initOption();
locationClient.setLocationOption(locationOption);
locationClient.startLocation();
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation.getErrorCode() == 0) {
final Map map = new HashMap<>();
//获取纬度
map.put("longitude", String.valueOf(aMapLocation.getLongitude()));
map.put("latitude", String.valueOf(aMapLocation.getLatitude()));
ToastUtil.showShort(MainBuildingActivity.this,"___"+aMapLocation.getCity());
Log.i("feng",aMapLocation.getCity());
}
}
private void initOption() {
// 设置是否须要显示地址信息
locationOption.setNeedAddress(true);
/**
* 设置是否优先返回GPS定位结果,若是30秒内GPS没有返回定位结果则进行网络定位
* 注意:只有在高精度模式下的单次定位有效,其余方式无效
*/
// locationOption.setGpsFirst(true);
// 设置是否开启缓存
locationOption.setLocationCacheEnable(true);
// 设置发送定位请求的时间间隔,最小值为1000,若是小于1000,按照1000算
locationOption.setInterval(5000);
locationOption.setOnceLocation(true);
}
五、根据经纬度在地图上显示的功能Activity
public class BuildingPostionActivity extends BaseActivity implements OnMapLoadedListener,AMap.OnMarkerClickListener {
private AMap aMap;
private MapView mapView;
private List latLngList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
mapView = findViewById(R.id.map);
mapView.onCreate(savedInstanceState); // 此方法必须重写
init();
}
/**
* 初始化AMap对象
*/
private void init() {
setTitle("楼盘位置",false,null);
registerBack();
if (aMap == null) {
aMap = mapView.getMap();
setUpMap();
}
}
private void setUpMap() {
aMap.setOnMapLoadedListener(BuildingPostionActivity.this);// 设置amap加载成功事件监听器
aMap.setOnMarkerClickListener(this);
// 往地图上添加位置标志marker
addMarkersToMap();
}
/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必须重写
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
/**
* 在地图上添加marker
*/
private void addMarkersToMap() {
//List positions = (List) getIntent().getSerializableExtra("positions");
List positions=new ArrayList<>();
BuildingBean bean=new BuildingBean();
bean.setLatitude("123.623458");
bean.setLongitude("41.780748");
bean.setBuildingName("滑翔小区");
positions.add(bean);
for (BuildingBean position:positions) {
LatLng latLng = new LatLng(Double.valueOf(position.getLongitude()),Double.valueOf(position.getLatitude()));
Marker marker = aMap.addMarker(new MarkerOptions().position(latLng).icon(
BitmapDescriptorFactory.fromResource(R.drawable.location)).title(position.getBuildingName()));
marker.showInfoWindow();
latLngList.add(latLng);
}
}
/**
* 对marker标注点点击响应事件
*/
@Override
public boolean onMarkerClick(final Marker marker) {
return false;
}
/**
* 监听amap地图加载成功事件回调
*/
@Override
public void onMapLoaded() {
// 设置全部maker显示在当前可视区域地图中
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng:latLngList) {
builder.include(latLng);
}
LatLngBounds build = builder.build();
aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(build, 10));
aMap.moveCamera(CameraUpdateFactory.zoomBy(4));
aMap.setOnMapLoadedListener(this);//设置地图显示监听器
}
}
(2)xml文件
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />