高德的账号去改的开房平台进行注册,注册成功后进入个人中心;
进入个人中心后点击应供管理
需要的权限有
清单文件中
sourceSets {
main {
jniLibs.srcDirs = [‘libs’]
}
}
写入一个工具类
public class LocationUtils implements AMapLocationListener {
private AMapLocationClient aMapLocationClient;
private AMapLocationClientOption clientOption;
private ILocationCallBack callBack;
public void startLocate(Context context) {
aMapLocationClient = new AMapLocationClient(context);
//设置监听回调
aMapLocationClient.setLocationListener(this);
//初始化定位参数
clientOption = new AMapLocationClientOption();
clientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
clientOption.setNeedAddress(true);
clientOption.setOnceLocation(false);
//设置是否强制刷新WIFI,默认为强制刷新
clientOption.setWifiActiveScan(true);
//设置是否允许模拟位置,默认为false,不允许模拟位置
clientOption.setMockEnable(false);
//设置定位间隔
clientOption.setInterval(2000);
aMapLocationClient.setLocationOption(clientOption);
aMapLocationClient.startLocation();
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
//定位成功完成回调
String country = aMapLocation.getCountry();
String province = aMapLocation.getProvince();
String city = aMapLocation.getCity();
String district = aMapLocation.getDistrict();
String street = aMapLocation.getStreet();
double lat = aMapLocation.getLatitude();
double lgt = aMapLocation.getLongitude();
callBack.callBack(country + province + city + district + street, lat, lgt, aMapLocation);
} else {
//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ aMapLocation.getErrorCode() + ", errInfo:"
+ aMapLocation.getErrorInfo());
}
}
}
/**
* 自定义图标
*
* @return
*/
public MarkerOptions getMarkerOption(String str, double lat, double lgt) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher));
markerOptions.position(new LatLng(lat, lgt));
markerOptions.title(str);
markerOptions.snippet("纬度:" + lat + " 经度:" + lgt);
markerOptions.period(100);
return markerOptions;
}
public interface ILocationCallBack {
void callBack(String str, double lat, double lgt, AMapLocation aMapLocation);
}
public void setLocationCallBack(ILocationCallBack callBack) {
this.callBack = callBack;
}
}
然后就是高德的代码
public class MainActivity extends AppCompatActivity implements LocationSource {
private MapView myMapView;
private AMap aMap;
private LocationSource.OnLocationChangedListener mListener = null;//定位监听器
private LocationUtils locationUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myMapView = findViewById(R.id.MapView);
myMapView.onCreate(savedInstanceState);
initView();
}
private void initView() {
if(aMap == null){
aMap = myMapView.getMap();
}
setLocationCallBack();
//设置定位监听
aMap.setLocationSource(this);
//设置缩放级别
aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
//显示定位层并可触发,默认false
aMap.setMyLocationEnabled(true);
}
private void setLocationCallBack(){
locationUtils = new LocationUtils();
locationUtils.setLocationCallBack(new LocationUtils.ILocationCallBack() {
@Override
public void callBack(String str,double lat,double lgt,AMapLocation aMapLocation) {
//根据获取的经纬度,将地图移动到定位位置
aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(lat,lgt)));
mListener.onLocationChanged(aMapLocation);
//添加定位图标
aMap.addMarker(locationUtils.getMarkerOption(str,lat,lgt));
}
});
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
mListener = onLocationChangedListener;
locationUtils.startLocate(getApplicationContext());
}
@Override
public void deactivate() {
mListener = null;
}
@Override
protected void onPause() {
super.onPause();
//暂停地图的绘制
myMapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
//销毁地图
myMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//重新绘制加载地图
myMapView.onResume();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
myMapView.onSaveInstanceState(outState);
}
}
xml中只需要写入一个MapView就ok
还有一个反编译地理位置的代码,效果图就不上了
public class AddrChangeActivity extends AppCompatActivity implements View.OnClickListener, GeocodeSearch.OnGeocodeSearchListener {
private EditText Addr_Edit;
private Button Zheng_Btn;
private EditText JinDU_Edit;
private EditText WeiDu_Edit;
private Button Fan_Btn;
private TextView Get_Text;
private GeocodeSearch geocodeSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addr_change);
initView();
//构造 GeocodeSearch 对象,并设置监听。
geocodeSearch = new GeocodeSearch(this);
geocodeSearch.setOnGeocodeSearchListener(this);
//通过GeocodeQuery设置查询参数,调用getFromLocationNameAsyn(GeocodeQuery geocodeQuery) 方法发起请求。
//address表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode都ok
// GeocodeQuery query = new GeocodeQuery(address, "010");
// geocoderSearch.getFromLocationNameAsyn(query);
}
private void initView() {
Addr_Edit = (EditText) findViewById(R.id.Addr_Edit);
Zheng_Btn = (Button) findViewById(R.id.Zheng_Btn);
JinDU_Edit = (EditText) findViewById(R.id.JinDU_Edit);
WeiDu_Edit = (EditText) findViewById(R.id.WeiDu_Edit);
Fan_Btn = (Button) findViewById(R.id.Fan_Btn);
Get_Text = (TextView) findViewById(R.id.Get_Text);
Zheng_Btn.setOnClickListener(this);
Fan_Btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Zheng_Btn:
String addr = Addr_Edit.getText().toString().trim();
if (addr.isEmpty()) {
Toast.makeText(this, "请输入地址", Toast.LENGTH_SHORT).show();
return;
}
//参数1:addr 地址值 参数2:规定一个区域
GeocodeQuery query = new GeocodeQuery(addr, null);
geocodeSearch.getFromLocationNameAsyn(query);
break;
case R.id.Fan_Btn:
String jingdu = JinDU_Edit.getText().toString().trim();
String weidu = WeiDu_Edit.getText().toString().trim();
if (jingdu.isEmpty() || weidu.isEmpty()) {
Toast.makeText(this, "请输入经纬度", Toast.LENGTH_SHORT).show();
return;
}
//这个是经纬度查询的类
LatLonPoint point = new LatLonPoint(Double.parseDouble(jingdu), Double.parseDouble(weidu));
RegeocodeQuery regeocodeQuery = new RegeocodeQuery(point, 2000000000, GeocodeSearch.AMAP);
GeocodeSearch reSe = new GeocodeSearch(this);
reSe.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
Log.e("onRegeocodeSearched", "onRegeocodeSearched");
RegeocodeAddress address = regeocodeResult.getRegeocodeAddress();
Get_Text.setText(address.getFormatAddress()+"地址");
}
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
}
});
geocodeSearch.getFromLocationAsyn(regeocodeQuery);
break;
}
}
//把经纬度转换成地址
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
Log.e("onRegeocodeSearched", "onRegeocodeSearched");
RegeocodeAddress address = regeocodeResult.getRegeocodeAddress();
Get_Text.setText(address.getFormatAddress());
}
//是吧地址转换成经度纬度
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
Log.e("onGeocodeSearched", "onGeocodeSearched");
//从查出来的结果集 得到地址对象
GeocodeAddress address = geocodeResult.getGeocodeAddressList().get(0);
//从地址对象里面得到 经纬度的类
LatLonPoint latLonPoint = address.getLatLonPoint();
//从这个point取经纬度即可
Get_Text.setText("经度是:" + latLonPoint.getLongitude() + ",纬度是:" + latLonPoint.getLatitude());
}
}
反编译的xml
以上;