配置
-
gradle
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}compile files('libs/AMap3DMap_5.1.0_AMapSearch_5.1.0_AMapLocation_3.4.0_20170518.jar')
权限
-
key和service
代码
- MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.SHAir.qualitytech.cn.R;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {
private TextView tv_default_address;//默认位置
private EditText et_input_address;//输入位置
private ImageView iv_input_delete;//删除按钮
private TextView tv_reload_address;//当前位置
private TextView tv_reload_location;//重新定位
private RecyclerView recycleView;
private TextView tv_current;
private TextView tv_nearby;
private LinearLayout ll_reload_location;
private ImageView iv_reload;
private ProgressBar progressBar;
//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
//声明AMapLocationClientOption对象
public AMapLocationClientOption mLocationOption = null;
private static double latitude;
private static double longitude;
private static String city;
private static String newText;
private static String cityCode;
private Ada_Change_Location adapter;
private ArrayList inputList;
private ArrayList nearByList;
private String itemAddress;
private String formatAddress;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView((R.layout.fragment_change_address);
adapter = new Ada_Change_Location(getActivity());
initWidget();
initLocationParam();
startLocate();
}
private void startLocate() {
//启动定位
mLocationClient.startLocation();
iv_reload.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}
public void initWidget() {
tv_default_address = (TextView) getView().findViewById(R.id.tv_default_address);
et_input_address = (EditText) getView().findViewById(R.id.et_input_address);
iv_input_delete = (ImageView) getView().findViewById(R.id.iv_input_delete);
tv_reload_address = (TextView) getView().findViewById(R.id.tv_reload_address);
tv_reload_location = (TextView) getView().findViewById(R.id.tv_reload_location);
recycleView = (RecyclerView) getView().findViewById(R.id.recyclerView_location);
tv_current = (TextView) getView().findViewById(R.id.tv_current);
tv_nearby = (TextView) getView().findViewById(R.id.tv_nearby);
ll_reload_location = (LinearLayout) getView().findViewById(R.id.ll_reload_location);
iv_reload = (ImageView) getView().findViewById(R.id.iv_reload);
progressBar= (ProgressBar) getView().findViewById(R.id.progressBar);
tv_reload_location.setOnClickListener(this);
et_input_address.addTextChangedListener(this);
iv_input_delete.setOnClickListener(this);
recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
recycleView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
}
/**
* 初始化定位相关配置
*/
private void initLocationParam() {
//初始化定位
mLocationClient = new AMapLocationClient(getContext());
mLocationOption = getDefaultOption();
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
//初始化AMapLocationClientOption对象
mLocationOption = new AMapLocationClientOption();
}
/**
* 默认的定位参数
*/
private AMapLocationClientOption getDefaultOption() {
AMapLocationClientOption mOption = new AMapLocationClientOption();
mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
return mOption;
}
/**
* 声明定位回调监听器
*/
private AMapLocationListener mLocationListener = new AMapLocationListener() {
public String address;
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (null != aMapLocation) {
if (aMapLocation.getErrorCode() == 0) {
iv_reload.setVisibility(View.VISIBLE);
iv_reload.setImageResource(R.mipmap.layer_4);
latitude = aMapLocation.getLatitude();//纬度
longitude = aMapLocation.getLongitude();//精度
final String province = aMapLocation.getProvince();//省信息
city = aMapLocation.getCity();//城市
cityCode = aMapLocation.getCityCode();//城市编码
final String district = aMapLocation.getDistrict();//城区信息
final String street = aMapLocation.getStreet();//街道信息
final String streetNum = aMapLocation.getStreetNum();//街道门牌号信息
final String aoiName = aMapLocation.getAoiName();//获取当前定位点的AOI信息
LatLonPoint latLonPoint = new LatLonPoint(latitude, longitude);
address = geocodeToAddress(latLonPoint);
progressBar.setVisibility(View.GONE);
if (address != null && province != null) {
tv_default_address.setText(province);
tv_reload_address.setText(address);
} else if (province != null && district != null && street != null && streetNum != null & aoiName != null) {
tv_default_address.setText(province);
tv_reload_address.setText(province + district + street + streetNum + aoiName);
} else {
tv_default_address.setText("定位失败");
tv_reload_address.setText("定位失败,请检查手机设置");
}
getNearBy();
} else {
tv_default_address.setText("定位失败");
tv_reload_address.setText("定位失败,请检查手机设置");
//定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ aMapLocation.getErrorCode() + ", errInfo:"
+ aMapLocation.getErrorInfo());
}
} else {
tv_default_address.setText("定位失败");
tv_reload_address.setText("定位失败,请检查手机设置");
}
}
};
/**
* 获取附近位置
*/
private void getNearBy() {
//搜索附近
initNearByParam();
adapter.setOnItemListener(new Ada_Change_Location.RecOnitem() {
@Override
public void OnChangeLocationClick(int position) {
if (nearByList != null && nearByList.size() > 0) {
String name = nearByList.get(position).name;
String nearBy = nearByList.get(position).address;
postAddress(name, nearBy);
}
}
});
}
/**
* 逆地理转换
*
* @return
*/
private String geocodeToAddress(LatLonPoint latLonPoint) {
//逆地理转换:经纬度转换为地址
GeocodeSearch geocoderSearch = new GeocodeSearch(getActivity());
geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int resultCode) {
if (resultCode == 1000 && regeocodeResult != null) {
RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
formatAddress = regeocodeAddress.getFormatAddress();
}
}
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
}
});
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);
geocoderSearch.getFromLocationAsyn(query);
return formatAddress;
}
/**
* 附件位置相关配置
*/
private void initNearByParam() {
if (cityCode != null) {
PoiSearch.Query query = new PoiSearch.Query("", "", cityCode);
query.setPageSize(30);
PoiSearch poiSearch = new PoiSearch(getContext(), query);
poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
@Override
public void onPoiSearched(PoiResult poiResult, int rCode) {
if (rCode == 1000 && poiResult != null && poiResult.getPois().size() > 0) {
ArrayList pois = poiResult.getPois();
nearByList = new ArrayList();
recycleView.setAdapter(adapter);
Collections.reverse(nearByList);
for (int i = 0; i < pois.size(); i++) {
String address = pois.get(i).getSnippet();
nearByList.add(new ChangeLocation(pois.get(i).getTitle(), address));
}
adapter.setdata(nearByList);
mLocationClient.stopLocation();//停止定位后,本地定位服务并不会被销毁
} else {
Log.i("附件=====", "搜索附近失败");
}
}
@Override
public void onPoiItemSearched(PoiItem poiItem, int i) {
}
});
poiSearch.searchPOIAsyn();
poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude, longitude), 1000));//设置周边搜索的中心点以及半径
}else{
ToastUtil.showToastShort(getContext(),"定位失败,无法获取附近位置");
}
}
/**
* 根据输入内容提示地址
*
* @param s
* @param start
* @param before
* @param count
*/
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (nearByList != null && nearByList.size() > 0) {
nearByList.clear();
}
tv_current.setVisibility(View.GONE);
ll_reload_location.setVisibility(View.GONE);
tv_nearby.setVisibility(View.GONE);
iv_input_delete.setVisibility(View.VISIBLE);
newText = s.toString().trim();
if (s != null && s.length() > 0) {
InputtipsQuery inputquery = new InputtipsQuery(newText, city);
inputquery.setCityLimit(true);
Inputtips inputTips = new Inputtips(getActivity(), inputquery);
inputTips.setInputtipsListener(new Inputtips.InputtipsListener() {
@Override
public void onGetInputtips(List tipList, int rCode) {
if (rCode == AMapException.CODE_AMAP_SUCCESS && tipList.size() > 0) {
inputList = new ArrayList();
recycleView.setAdapter(adapter);
Collections.reverse(inputList);
for (int i = 0; i < tipList.size(); i++) {
inputList.add(new ChangeLocation(tipList.get(i).getName(), tipList.get(i).getAddress()));
}
adapter.setdata(inputList);
} else {
Log.e("InputtipsQuery==", rCode + "");
}
}
});
inputTips.requestInputtipsAsyn();
} else {
getNearBy();
tv_current.setVisibility(View.VISIBLE);
ll_reload_location.setVisibility(View.VISIBLE);
tv_nearby.setVisibility(View.VISIBLE);
iv_input_delete.setVisibility(View.GONE);
}
}
@Override
public void afterTextChanged(Editable s) {
String string = s.toString();
if (!TextUtils.isEmpty(string)) {
adapter.setOnItemListener(new Ada_Change_Location.RecOnitem() {
@Override
public void OnChangeLocationClick(int position) {
if (inputList != null && inputList.size() > 0) {
String name = inputList.get(position).name;
itemAddress = inputList.get(position).address;
postAddress(name, itemAddress);
}
}
});
} else {
getNearBy();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_reload_location:
//重新定位
startLocate();
break;
case R.id.iv_input_delete:
et_input_address.setText("");
iv_input_delete.setVisibility(View.GONE);
getNearBy();
break;
}
}
/**
* 上传地址
*
* @param address
*/
private void postAddress(final String name, final String address) {
if (TextUtils.isEmpty(address)) {
Toast.makeText(this, "地址不能为空",Toast.SHROT_LENGTH).show();
} else {
//上传代码
}
}
@Override
public void onDestroy() {
super.onDestroy();
mLocationClient.onDestroy();//销毁定位客户端,同时销毁本地定位服务。
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
}
- Ada_Change_Location
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.SHAir.qualitytech.cn.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by QualityTech on 2017/6/3.
*/
public class Ada_Change_Location extends RecyclerView.Adapter {
Context context;
RecOnitem recOnitem;
List list = new ArrayList<>();
private ChangeLocationHolder changeHoder;
public void setdata(List list) {
this.list.clear();
this.list = list;
notifyDataSetChanged();
}
public void setOnItemListener(RecOnitem recOnitem) {
this.recOnitem = recOnitem;
}
public Ada_Change_Location(Context context) {
this.context = context;
}
@Override
public ChangeLocationHolder onCreateViewHolder(ViewGroup parent, int viewType) {
changeHoder = new ChangeLocationHolder(LayoutInflater.from(context).inflate(R.layout.gaode_item_layout, null));
return changeHoder;
}
@Override
public void onBindViewHolder(ChangeLocationHolder holder, final int position) {
holder.poi_field_id.setText(list.get(position).name);
holder.poi_value_id.setText(list.get(position).address);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recOnitem.OnChangeLocationClick(position);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class ChangeLocationHolder extends RecyclerView.ViewHolder {
ImageView iv_nearyby_layer;
TextView poi_field_id;
TextView poi_value_id;
public ChangeLocationHolder(View view) {
super(view);
iv_nearyby_layer = (ImageView) view.findViewById(R.id.iv_nearyby_layer);
poi_field_id = (TextView) view.findViewById(R.id.poi_field_id);
poi_value_id = (TextView) view.findViewById(R.id.poi_value_id);
}
}
public interface RecOnitem {
public void OnChangeLocationClick(int position);
}
}
- ChangeLocation
public class ChangeLocation {
public String name;
public String address;
public ChangeLocation(String name, String address) {
this.name = name;
this.address = address;
}
}
- activity_main
- gaode_item_layout
- location_shape