高德定位,高德地图实例

    xiaoxiao2021-09-19  95

         前几天做了个添加地址的页面,其中用到了定位和地图,先上高德的定位和地图:

         效果:

         进入页面后,自动定位,如果定位不成功,可以点击“重新定位”的按钮重新定位,在位置显示框里输入地址可以以下拉框的形式显示出相关的地址列表,点击其中一条,在地图上显示相应地图。地图可以拖拉,绽放。

       上代码:

         

    package com.haier.mine_wine.address.addrEdit; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.AutoCompleteTextView; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; 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.maps2d.AMap; import com.amap.api.maps2d.CameraUpdateFactory; import com.amap.api.maps2d.MapView; import com.amap.api.maps2d.UiSettings; import com.amap.api.maps2d.model.BitmapDescriptorFactory; import com.amap.api.maps2d.model.LatLng; import com.amap.api.maps2d.model.Marker; import com.amap.api.maps2d.model.MarkerOptions; import com.amap.api.services.core.LatLonPoint; import com.amap.api.services.geocoder.GeocodeAddress; import com.amap.api.services.geocoder.GeocodeResult; import com.amap.api.services.geocoder.GeocodeSearch; 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.haier.mine_wine.R; import com.haier.mine_wine.R2; import com.haier.mine_wine.address.addAddr.bean.AddDataBean; import com.haier.mine_wine.address.addrmanage.bean.AddrDetail; import com.haier.mine_wine.address.addrmanage.bean.AddressInfo; import com.haier.mine_wine.address.addrmanage.bean.EditDataBean; import com.haier.mine_wine.address.addrmanage.bean.SelDataBean; import com.haier.mine_wine.address.addrmanage.contract.IAddrManageView; import com.haier.mine_wine.address.addrmanage.presenter.AddrManagePresenter; import com.haier.mine_wine.address.common.AddressUtil; import com.haier.mine_wine.address.common.AutoCompleteAdapter; import com.haier.mine_wine.util.HttpService; import com.haier.wine_commen.html.ServiceAddr; import com.haier.wine_commen.util.CustomRetrofit; import com.haier.wine_commen.util.UserLoginConfig; import com.orhanobut.logger.Logger; import org.json.JSONException; import org.json.JSONObject; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import okhttp3.Response; import retrofit2.Call; import retrofit2.Callback; /* * 地址编辑页 * Created by wangchm on 2016/9/27 0027. * */ public class AddrEditActivity extends Activity implements TextWatcher, Inputtips.InputtipsListener, GeocodeSearch.OnGeocodeSearchListener,IAddrManageView { private static final String TAG = AddrEditActivity.class.getSimpleName(); private Context mContext; // private static final String KEY = "d653b22472df13b7c9d36df4684ad711"; //姓名 @BindView(R2.id.et_eName) EditText etEName; //电话 @BindView(R2.id.et_eTel) EditText etETel; //地址 @BindView(R2.id.et_eAddr) AutoCompleteTextView etEAddr; //是否设为默认地址 @BindView(R2.id.iv_eDefault) ImageView ivEDefault; //删除 @BindView(R2.id.btn_eDel) TextView btnEDel; //保存 @BindView(R2.id.btn_eSave) TextView btnESave; @BindView(R2.id.iv_editBack) ImageView ivEditBack; @BindView(R2.id.iv_backTohome) ImageView ivBackTohome; // AddrDetail addrDetail; AddressInfo addressInfo; //定位按钮 @BindView(R2.id.tv_fix) TextView tvFix; //详细地址 @BindView(R2.id.et_addrDetail) EditText etAddrDetail; //地图 @BindView(R2.id.gaodeMap) MapView gaodeMap; private AMap aMap; //声明AMapLocationClient类对象 public AMapLocationClient mLocationClient = null; //声明mLocationOption对象 public AMapLocationClientOption mLocationOption = null; private UiSettings mUiSettings; private Double lat; private Double lon; private String mCity = ""; private String areaInfo=""; private String adCode = ""; private String address = ""; private AutoCompleteAdapter adapter; private GeocodeSearch geocoderSearch; private Marker geoMarker; private Marker regeoMarker; AddrManagePresenter presenter; private String isDefault = "0"; HttpService httpService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_addr_edit); ButterKnife.bind(this); mContext = AddrEditActivity.this; presenter = new AddrManagePresenter(this); initData(); gaodeMap.onCreate(savedInstanceState); //初始化定位 mLocationClient = new AMapLocationClient(getApplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(mLocationListener); init(); httpService = CustomRetrofit.getInstance().getRetrofit(ServiceAddr.BASE_URL).create(HttpService.class); } /* * 获取传递过来的地址信息,并显示 * */ public void initData() { if (getIntent().getSerializableExtra("addrinfo") != null) { addressInfo = (AddressInfo) getIntent().getSerializableExtra("addrinfo"); } etEName.setText(addressInfo.getTrue_name()); etETel.setText(addressInfo.getMob_phone()); etAddrDetail.setText(addressInfo.getAddress()); isDefault = addressInfo.getIs_default(); if (addressInfo.getIs_default().equals("1")) { ivEDefault.setSelected(true); } else { ivEDefault.setSelected(false); } etEAddr.addTextChangedListener(this); etEAddr.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean b) { if (b) { autoInvokeTextWatcher(); } } }); etEAddr.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { autoInvokeTextWatcher(); } }); etEAddr.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Tip t = adapter.getItem(i); getAddress(t.getPoint()); } }); } /** * * 初始化AMap对象 */ private void init() { if (aMap == null) { aMap = gaodeMap.getMap(); } setUpMap(); geocoderSearch = new GeocodeSearch(this); geocoderSearch.setOnGeocodeSearchListener(this); } private void setUpMap() { //初始化定位参数 mLocationOption = new AMapLocationClientOption(); //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //设置是否只定位一次,默认为false mLocationOption.setOnceLocation(true); //设置是否强制刷新WIFI,默认为强制刷新 mLocationOption.setWifiActiveScan(true); //设置是否允许模拟位置,默认为false,不允许模拟位置 mLocationOption.setMockEnable(false); //设置定位间隔,单位毫秒,默认为2000ms // mLocationOption.setInterval(2000); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); //启动定位 mLocationClient.startLocation(); //重新定位时移除红气泡,再重新初始化 geoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f) .icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_BLUE))); regeoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f) .icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_RED))); } //声明定位回调监听器 public AMapLocationListener mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //定位成功回调信息,设置相关消息 amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表 amapLocation.getLatitude();//获取纬度 amapLocation.getLongitude();//获取经度 amapLocation.getAccuracy();//获取精度信息 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(amapLocation.getTime()); df.format(date);//定位时间 amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。 amapLocation.getCountry();//国家信息 amapLocation.getProvince();//省信息 mCity = amapLocation.getCity();//城市信息 amapLocation.getDistrict();//城区信息 amapLocation.getStreet();//街道信息 amapLocation.getStreetNum();//街道门牌号信息 amapLocation.getCityCode();//城市编码 adCode = amapLocation.getAdCode();//地区编码 amapLocation.getAoiName();//获取当前定位点的AOI信息 lat = amapLocation.getLatitude(); lon = amapLocation.getLongitude(); areaInfo = amapLocation.getProvince()+" "+amapLocation.getCity() +" "+amapLocation.getDistrict(); Log.v("pcw", "lat : " + lat + " lon : " + lon+" adCode: "+adCode); etEAddr.setText(amapLocation.getAoiName()); // 设置当前地图显示为当前位置 aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 16)); MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(new LatLng(lat, lon)); markerOptions.title("当前位置"); markerOptions.visible(true); // BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.location_marker)); // markerOptions.icon(bitmapDescriptor); aMap.addMarker(markerOptions); } else { //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。 Log.e("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); Toast.makeText(mContext,amapLocation.getErrorInfo(),Toast.LENGTH_LONG).show(); } } } }; @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 gaodeMap.onDestroy(); mLocationClient.onDestroy();//销毁定位客户端,同时销毁本地定位服务。 } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理 gaodeMap.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理 gaodeMap.onPause(); mLocationClient.stopLocation();//停止定位后,本地定位服务并不会被销毁 } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),实现地图生命周期管理 gaodeMap.onSaveInstanceState(outState); } @OnClick(R2.id.tv_fix) public void fixOnClick() { //移除红气泡 regeoMarker.remove(); setUpMap(); } /* * 删除地址 * */ @OnClick(R2.id.btn_eDel) public void delOnClick() { HashMap<String,String> map = new HashMap<String,String>(); map.put("key",UserLoginConfig.getAccessToken()); map.put("address_id",addressInfo.getAddress_id()); presenter.delAddr(ServiceAddr.BASE_URL,map); // presenter.delAddr2(ServiceAddr.BASE_URL,map); } /* * 保存地址 * */ @OnClick(R2.id.btn_eSave) public void saveOnClick() { if(adCode != null && !adCode.equals("")){ String tel = etETel.getText().toString(); if(!AddressUtil.isPhone(mContext,tel)){ Toast.makeText(mContext,"手机号格式错误,应为11位纯数字",Toast.LENGTH_LONG).show(); }else if(!AddressUtil.isPhone2(mContext,tel)){ Toast.makeText(mContext,"手机号非法。请输入移动、联通、电信手机号,不支持小灵通",Toast.LENGTH_LONG).show(); }else{ HashMap<String,String> map = new HashMap<String, String>(); map.put("key",UserLoginConfig.getAccessToken()); map.put("true_name",etEName.getText().toString()); String id = adCode.substring(0,4); map.put("city_id",id+"002"); map.put("area_id",id+"003"); map.put("area_info",areaInfo); map.put("address",etAddrDetail.getText().toString()); map.put("tel_phone",tel); map.put("mob_phone",tel); map.put("address_id",addressInfo.getAddress_id()); map.put("is_default",isDefault); presenter.editAddr(ServiceAddr.BASE_URL,map); } }else{ Toast.makeText(mContext,"定位失败,请重新定位",Toast.LENGTH_LONG).show(); } } /* * 点击屏幕其他地方使软键盘消失 * */ @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { //隐藏软键盘 if ((null != getCurrentFocus()) && (null != getCurrentFocus().getWindowToken())) { InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } return super.dispatchTouchEvent(ev); } /* * AutoCompleteTextView内容改变时的监听 * */ @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { String newText = charSequence.toString().trim(); if (!AddressUtil.IsEmptyOrNullString(newText)) { InputtipsQuery inputquery = new InputtipsQuery(newText, mCity); Inputtips inputTips = new Inputtips(mContext, inputquery); inputTips.setInputtipsListener(this); inputTips.requestInputtipsAsyn(); } } @Override public void afterTextChanged(Editable editable) { } /* * AutoCompleteTextView的弹出框 * */ @Override public void onGetInputtips(List<Tip> list, int i) { if (i == 1000) {// 正确返回 Logger.t(TAG).d("list.size()=" + list.size() + " " + list.get(0).getDistrict() + " " + list.get(0).getAddress()); List<Tip> l = new ArrayList<Tip>(); for (Tip t : list) { if (null != t.getPoint()) { l.add(t); } } // mList = l; adapter = new AutoCompleteAdapter(this, l); etEAddr.setAdapter(adapter); autoInvokeTextWatcher(); } else { Toast.makeText(mContext, i, Toast.LENGTH_LONG).show(); } } /** * 反射手动调用afterTextChanged, 目的是为了让Autoxxx默认弹出popwin */ private void autoInvokeTextWatcher() { final AutoCompleteTextView view = etEAddr; Field f; for (Class<?> klass = view.getClass(); !klass.equals(View.class); klass = klass.getSuperclass()) { try { f = klass.getDeclaredField("mListeners"); if (!f.getType().equals(ArrayList.class)) { return; } f.setAccessible(true); Object res = f.get(view); if (res != null && res instanceof ArrayList) { ArrayList<TextWatcher> listeners = (ArrayList<TextWatcher>) res; if (!listeners.isEmpty()) { listeners.get(0).afterTextChanged(null); return; } } } catch (NoSuchFieldException e) { e.printStackTrace(); continue; } catch (IllegalAccessException e) { e.printStackTrace(); return; } } } /** * 响应逆地理编码 */ public void getAddress(final LatLonPoint latLonPoint) { // ShowLoadingUtil.showProgressDialog2(this, "正在定位"); RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 geocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求 } @Override public void onRegeocodeSearched(RegeocodeResult result, int rCode) { if (rCode == 1000) { if (result != null && result.getRegeocodeAddress() != null && result.getRegeocodeAddress().getFormatAddress() != null) { // addressName = result.getRegeocodeAddress().getFormatAddress() // + "附近"; // result.getRegeocodeQuery().getPoint() adCode = result.getRegeocodeAddress().getAdCode(); areaInfo = result.getRegeocodeAddress().getProvince()+" " + result.getRegeocodeAddress().getCity() + " " + result.getRegeocodeAddress().getDistrict(); Logger.t(TAG).d("adCode="+adCode+" areaInfo = "+areaInfo); aMap.animateCamera(CameraUpdateFactory.newLatLngZoom( AddressUtil.convertToLatLng(result.getRegeocodeQuery().getPoint()), 15)); regeoMarker.setPosition(AddressUtil.convertToLatLng(result.getRegeocodeQuery().getPoint())); // result.getRegeocodeAddress().getDistrict() // if (!tag && result.getRegeocodeAddress().getAois() != null && result.getRegeocodeAddress().getAois().size() > 0) { // tag = true; // mName = result.getRegeocodeAddress().getAois().get(0).getAoiName(); // etVillage.setText(mName); // } Toast.makeText(mContext, "定位成功", Toast.LENGTH_LONG).show(); } else { // ToastUtil.show(GeocoderActivity.this, R.string.no_result); Toast.makeText(mContext, "定位失败", Toast.LENGTH_LONG).show(); } } else { // ToastUtil.showerror(GeocoderActivity.this, rCode); Toast.makeText(mContext, rCode, Toast.LENGTH_LONG).show(); } } @Override public void onGeocodeSearched(GeocodeResult result, int rCode) { if (rCode == 1000) { if (result != null && result.getGeocodeAddressList() != null && result.getGeocodeAddressList().size() > 0) { GeocodeAddress address = result.getGeocodeAddressList().get(0); aMap.animateCamera(CameraUpdateFactory.newLatLngZoom( AddressUtil.convertToLatLng(address.getLatLonPoint()), 15)); geoMarker.setPosition(AddressUtil.convertToLatLng(address .getLatLonPoint())); // addressName = "经纬度值:" + address.getLatLonPoint() + "\n位置描述:" // + address.getFormatAddress(); Toast.makeText(mContext, "定位成功", Toast.LENGTH_LONG).show(); } else { Toast.makeText(mContext, "定位失败", Toast.LENGTH_LONG).show(); } } else { Toast.makeText(mContext, rCode, Toast.LENGTH_LONG).show(); } } @OnClick(R2.id.iv_editBack) public void backOnClick() { finish(); } @OnClick(R2.id.iv_backTohome) public void homeBackOnClick() { Intent intent = new Intent(); intent.setAction("com.haier.wine.mian"); startActivity(intent); } /* * 设为默认地址 * */ @OnClick(R2.id.iv_eDefault) public void checkOnClick(){ if(isDefault.equals("1")){ ivEDefault.setSelected(false); isDefault = "0"; }else{ isDefault = "1"; ivEDefault.setSelected(true); } } @Override public void showAddrList(SelDataBean bean) { } /* * 修改地址后的提示信息 * */ @Override public void showEditAddr(String data) { try { JSONObject object = new JSONObject(data); if(object.getInt("code") == 200){ Toast.makeText(mContext,"修改成功",Toast.LENGTH_LONG).show(); finish(); }else if(object.getInt("code") == 400){ Toast.makeText(mContext,object.getJSONObject("datas").getString("error"),Toast.LENGTH_LONG).show(); } } catch (JSONException e) { e.printStackTrace(); } } /* * 删除地址后的提示信息 * */ @Override public void delAddrShow(String data) { try { JSONObject object = new JSONObject(data); if(object.getInt("code") == 200){ Toast.makeText(mContext,"删除成功",Toast.LENGTH_LONG).show(); finish(); }else if(object.getInt("code") == 400){ Toast.makeText(mContext,object.getJSONObject("datas").getString("error"),Toast.LENGTH_LONG).show(); } } catch (JSONException e) { e.printStackTrace(); } Logger.t(TAG).d(data.toString()); } @Override public void delOnFailure(Throwable throwable) { Toast.makeText(mContext,"网络连接异常,请稍候再试",Toast.LENGTH_LONG).show(); } @Override public void onFailure(Throwable e) { Toast.makeText(mContext,"网络连接异常,请稍候再试",Toast.LENGTH_LONG).show(); } } 布局文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/px_30" android:paddingLeft="@dimen/px_30" android:paddingRight="@dimen/px_30" android:paddingTop="@dimen/px_100" tools:context="com.haier.mine_wine.address.addAddr.AddAddrActivity"> <LinearLayout android:orientation="vertical" android:layout_above="@+id/rl_addBottom" android:background="@drawable/bg" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/ll_addHead" android:layout_width="match_parent" android:layout_height="@dimen/px_130"> <TextView android:text="编辑地址" android:layout_centerInParent="true" style="@style/addr_item_style" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/btn_eDel" android:text="删除" android:background="@drawable/corner_orange_button" android:gravity="center" android:textColor="@color/white" android:textSize="@dimen/px_20" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/btn_eSave" android:layout_marginRight="@dimen/px_78" android:layout_width="@dimen/mine_button_width" android:layout_height="@dimen/px_50" /> <TextView android:id="@+id/btn_eSave" android:text="保存" android:background="@drawable/corner_orange_button" android:gravity="center" android:textColor="@color/white" android:textSize="@dimen/px_20" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_marginRight="@dimen/px_30" android:layout_width="@dimen/mine_button_width" android:layout_height="@dimen/px_50" /> </RelativeLayout> <View android:background="@color/color_d5d5d5" android:layout_marginLeft="@dimen/px_30" android:layout_marginRight="@dimen/px_30" android:layout_width="match_parent" android:layout_height="0.3dp"></View> <LinearLayout android:id="@+id/ll_detail" android:orientation="vertical" android:layout_marginLeft="@dimen/px_30" android:layout_marginRight="@dimen/px_30" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="@dimen/addr_detail_width" android:layout_height="wrap_content"> <LinearLayout android:layout_marginTop="@dimen/px_24" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="收货人:" style="@style/addr_item_style" android:layout_marginRight="@dimen/px_22" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_eName" android:background="@drawable/ll_orange_border" android:paddingLeft="@dimen/px_10" style="@style/addr_item_style" android:layout_marginRight="@dimen/addr_margin_right" android:layout_width="@dimen/text_width" android:layout_height="@dimen/px_52" /> <TextView android:text="电话:" style="@style/addr_item_style" android:layout_marginLeft="@dimen/px_16" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_eTel" android:paddingLeft="@dimen/px_10" android:inputType="phone" android:background="@drawable/ll_orange_border" style="@style/addr_item_style" android:layout_width="@dimen/text_width" android:layout_height="@dimen/px_52" /> </LinearLayout> <LinearLayout android:layout_marginTop="@dimen/px_24" android:layout_width="match_parent" android:layout_height="@dimen/px_52"> <TextView android:text="所在地区:" style="@style/addr_item_style" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RelativeLayout android:background="@drawable/ll_orange_border" android:layout_width="match_parent" android:layout_height="wrap_content"> <AutoCompleteTextView android:id="@+id/et_eAddr" android:background="@null" android:layout_toLeftOf="@+id/view1" android:paddingLeft="@dimen/px_10" style="@style/addr_item_style" android:layout_width="match_parent" android:layout_height="@dimen/px_52" /> <View android:id="@+id/view1" android:layout_toLeftOf="@+id/tv_fix" android:background="@color/color_orange" android:layout_width="0.3dp" android:layout_height="match_parent"/> <TextView android:id="@+id/tv_fix" android:text="重新定位" style="@style/addr_item_style" android:gravity="center_vertical" android:layout_alignParentRight="true" android:drawableLeft="@drawable/location" android:layout_marginRight="@dimen/px_16" android:layout_marginLeft="@dimen/px_16" android:drawablePadding="@dimen/px_10" android:layout_width="wrap_content" android:layout_height="match_parent" /> </RelativeLayout> </LinearLayout> <LinearLayout android:layout_marginTop="@dimen/px_24" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="详细地址:" style="@style/addr_item_style" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_addrDetail" android:paddingLeft="@dimen/px_10" android:background="@drawable/ll_orange_border" style="@style/addr_item_style" android:layout_width="match_parent" android:layout_height="@dimen/px_52" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_marginTop="@dimen/px_20" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.amap.api.maps2d.MapView android:id="@+id/gaodeMap" android:layout_width="match_parent" android:layout_height="@dimen/addr_margin_right"> </com.amap.api.maps2d.MapView> </LinearLayout> <LinearLayout android:layout_marginTop="@dimen/px_42" android:layout_marginBottom="@dimen/px_30" android:layout_marginLeft="5px" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_eDefault" android:layout_marginRight="@dimen/px_16" android:src="@drawable/addr_default" android:layout_width="@dimen/px_30" android:layout_height="@dimen/px_30" /> <TextView android:text="设为默认" style="@style/addr_sub_item_style" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </LinearLayout> <RelativeLayout android:id="@+id/rl_addBottom" android:gravity="center_vertical" android:layout_alignParentBottom="true" android:layout_marginTop="@dimen/px_20" android:layout_width="match_parent" android:layout_height="@dimen/px_92"> <ImageView android:id="@+id/iv_editBack" android:src="@drawable/common_back" android:layout_width="@dimen/px_92" android:layout_height="@dimen/px_92" /> <ImageView android:id="@+id/iv_backTohome" android:src="@drawable/home" android:layout_toRightOf="@+id/iv_editBack" android:layout_marginLeft="@dimen/px_10" android:layout_width="@dimen/px_92" android:layout_height="@dimen/px_92" /> </RelativeLayout> </RelativeLayout> 配置: 在build.gradle中的dependencies加入//高德地图 compile files('libs/Amap_2DMap_V2.9.1_20160825.jar') compile files('libs/AMap_Location_V2.4.1_20160414.jar') compile files('libs/AMap_Search_V3.5.0_20160927.jar') 在AndroidMenifest.xml中加入相应权限:<!-- 高德定位 --> <!-- 用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <!-- 用于访问GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 --> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!-- 用于访问网络,网络定位需要上网 --> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <!-- 用于读取手机当前的状态 --> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <!-- 写入扩展存储,向扩展卡写入数据,用于写入缓存定位数据 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 并配置相应的key及service:<!-- 测试版 高德地图 key --> <meta-data android:name="com.amap.api.v2.apikey" android:value="******************"></meta-data> <service android:name="com.amap.api.location.APSService" />

    转载请注明原文地址: https://ju.6miu.com/read-677725.html

    最新回复(0)