清单文件里<meta-data android:name="com.amap.api.v2.apikey" android:value="3d0e16df0cfdfc8e781c480ee17a84d9自己申请的key"/>
<service android:name="com.amap.api.location.APSService"></service>
<!--用于进行网络定位--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <!--用于访问GPS定位--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <!--用于获取运营商信息,用于支持提供运营商信息相关的接口--> <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> <!--用于访问网络,网络定位需要上网--> <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> <!--用于申请调用A-GPS模块--> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission> <!--用于申请获取蓝牙信息进行室内定位--> <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--允许程序访问WiFi网络信息--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!--允许程序读写手机状态和身份--> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!--允许程序访问CellID或WiFi热点来获取粗略的位置--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
gradle里依赖
compile 'com.google.code.gson:gson:2.6.2' compile 'com.squareup.okhttp3:okhttp:3.3.0' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.jcodecraeer:xrecyclerview:1.5.9' compile 'com.amap.api:3dmap:5.0.0' compile 'com.amap.api:location:3.3.0' compile 'com.amap.api:search:5.0.0'
主布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/recylerview"/> </LinearLayout>
view包
public interface IMainView { //显示商品列表的方法 void showGoodsList(List<GoodListBean.DataBean> list); }
public class MainActivity extends AppCompatActivity implements IMainView{ private XRecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); PresenterImpl presenter = new PresenterImpl(); presenter.showGoodsListToView(new ModelImpl(),this); } private void initViews() { recyclerView = findViewById(R.id.recylerview); recyclerView.setLayoutManager(new LinearLayoutManager(this)); } @Override public void showGoodsList(final List<GoodListBean.DataBean> list) { MyAdapter myAdapter = new MyAdapter(MainActivity.this, list); // recyclerView.setAdapter(myAdapter); myAdapter.setMyItemClickListener(new MyAdapter.MyItemClickListener() { @Override public void onItemClick(View view, int position) { Intent intent=new Intent(MainActivity.this,DetailActivity.class); String s = list.get(position).getImages().split("\\|")[0]; intent.putExtra("s",s); intent.putExtra("title",list.get(position).getTitle()); startActivity(intent); finish(); } }); recyclerView.setAdapter(myAdapter); } }model层里
public interface IModel { //获取商品数据 void getGoodsList(String url,GetGoodsListener getGoodsListener); }
public class ModelImpl implements IModel { @Override public void getGoodsList(String url, final GetGoodsListener getGoodsListener) { OkHttpUtils okHttpUitls = OkHttpUtils.getOkHttpUitls(); okHttpUitls.okGet(url); okHttpUitls.setOkLoadListener(new OkLoadListener() { @Override public void okLoadSuccess(String json) { getGoodsListener.getGoodsSuccess(json); } @Override public void okLoadError(String error) { getGoodsListener.getGoodsError(error); } }); } }
public interface GetGoodsListener { void getGoodsSuccess(String json); void getGoodsError(String error); }presenter层
public interface IPresenter { void showGoodsListToView(IModel iModel, IMainView iMainView); }
public class PresenterImpl implements IPresenter{ @Override public void showGoodsListToView(IModel iModel, final IMainView iMainView) { String url= HttpConfig.url+"?pscid=1"; iModel.getGoodsList(url, new GetGoodsListener() { @Override public void getGoodsSuccess(String json) { GoodListBean goodListBean = new Gson().fromJson(json, GoodListBean.class); List<GoodListBean.DataBean> list = goodListBean.getData(); iMainView.showGoodsList(list); } @Override public void getGoodsError(String error) { } }); } }详情页面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.dell.gaode_xiangqing.view.DetailActivity"> <ImageView android:scaleType="centerCrop" android:id="@+id/detail_pic" android:layout_width="match_parent" android:layout_height="300dp" /> <TextView android:id="@+id/detail_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="商品名"/> <TextView android:text="送至:" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/detail_address"/> </LinearLayout>
public class DetailActivity extends AppCompatActivity { private static final String TAG = "DetailActivity"; private ImageView detail_pic; private TextView detail_title; private TextView detail_address; public AMapLocationClient mLocationClient = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); initViews(); initDatas(); initApi(); } private void initApi() { //声明AMapLocationClient类对象 //初始化定位 mLocationClient = new AMapLocationClient(getApplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(mAMapLocationListener); AMapLocationClientOption mLocationOption = null; //初始化AMapLocationClientOption对象 mLocationOption = new AMapLocationClientOption(); AMapLocationClientOption option = new AMapLocationClientOption(); /** * 设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景) */ // option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn); if (null != mLocationClient) { mLocationClient.setLocationOption(option); //设置场景模式后最好调用一次stop,再调用start以保证场景模式生效 mLocationClient.stopLocation(); mLocationClient.startLocation(); } //定位模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。 mLocationOption.setInterval(1000); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); //启动定位 mLocationClient.startLocation(); } AMapLocationListener mAMapLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation amapLocation) { //获取数据的 if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //可在其中解析amapLocation获取相应内容。 Log.d(TAG, "定位成功----"); String street = amapLocation.getStreet(); String address = amapLocation.getAddress(); Log.d(TAG, "onLocationChanged: "+street); Log.d(TAG, "onLocationChanged: "+address); //将地址显示出来 detail_address.setText(address); } else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 Log.e("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); } } } }; private void initDatas() { Intent intent = getIntent(); String pic_url = intent.getStringExtra("s"); String title = intent.getStringExtra("title"); Glide.with(DetailActivity.this).load(pic_url).into(detail_pic); detail_title.setText(title); } private void initViews() { detail_pic = findViewById(R.id.detail_pic); detail_title = findViewById(R.id.detail_title); detail_address = findViewById(R.id.detail_address); detail_address.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(DetailActivity.this,MyAddressActivity.class); startActivity(intent); } }); } }
适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private Context context; private List<GoodListBean.DataBean> list; private MyItemClickListener myItemClickListener; public MyAdapter(Context context, List<GoodListBean.DataBean> list) { this.context = context; this.list = list; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.xrecycler_layout, null); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, final int position) { ImageView recycler_icon = holder.getRecycler_icon(); TextView recycler_title = holder.getRecycler_title(); Glide.with(context).load(list.get(position).getImages().split("\\|")[0]).into(recycler_icon); recycler_title.setText(list.get(position).getTitle()); //条目点击事件 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { myItemClickListener.onItemClick(view,position); } }); } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ private final TextView recycler_title; private final ImageView recycler_icon; public MyViewHolder(View itemView) { super(itemView); recycler_title = itemView.findViewById(R.id.recycler_title); recycler_icon = itemView.findViewById(R.id.recycler_icon); } public TextView getRecycler_title() { return recycler_title; } public ImageView getRecycler_icon() { return recycler_icon; } } //自定义条目点击事件 public interface MyItemClickListener{ void onItemClick(View view,int position); } public void setMyItemClickListener(MyItemClickListener myItemClickListener){ this.myItemClickListener=myItemClickListener; } }
地图地址界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dell.gaode_xiangqing.view.MyAddressActivity"> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
public class MyAddressActivity extends AppCompatActivity { //声明AMapLocationClientOption对象 public AMapLocationClientOption mLocationOption = null; //声明AMapLocationClient类对象 public AMapLocationClient mLocationClient = null; //声明定位回调监听器 public AMapLocationListener mLocationListener = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_address); initGaoDe();// 定位做完了 // 显示地图 MapView mMapView = (MapView) findViewById(R.id.map); //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图 mMapView.onCreate(savedInstanceState); } public void initGaoDe() { //可以通过类implement方式实现AMapLocationListener接口,也可以通过创造接口类对象的方法实现 mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //可在其中解析amapLocation获取相应内容。 String province = amapLocation.getProvince();//省信息 String city = amapLocation.getCity();//城市信息 String district = amapLocation.getDistrict();//城区信息 String street = amapLocation.getStreet();//街道信息 // 这里是定位到的地址 String userAddr = province + " " + city + " " + district + " " + street; Log.e("userAddr", userAddr); // 还可以获取其他的信息 amapLocation对象里面 } else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 Log.e("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); } } } }; //初始化定位 mLocationClient = new AMapLocationClient(getApplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(mLocationListener); //初始化AMapLocationClientOption对象 mLocationOption = new AMapLocationClientOption(); AMapLocationClientOption option = new AMapLocationClientOption(); /** * 设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景) */ // option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn); if (null != mLocationClient) { mLocationClient.setLocationOption(option); //设置场景模式后最好调用一次stop,再调用start以保证场景模式生效 mLocationClient.stopLocation(); mLocationClient.startLocation(); } //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。 //mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving); //设置定位模式为AMapLocationMode.Device_Sensors,仅设备模式。(需要到开阔地带) //mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Device_Sensors); //获取一次定位结果: //该方法默认为false。 mLocationOption.setOnceLocation(true); //获取最近3s内精度最高的一次定位结果: //设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。 mLocationOption.setOnceLocationLatest(true); //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。 mLocationOption.setInterval(1000); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //设置是否允许模拟位置,默认为true,允许模拟位置 mLocationOption.setMockEnable(true); //单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。 mLocationOption.setHttpTimeOut(20000); //关闭缓存机制 mLocationOption.setLocationCacheEnable(false); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); //启动定位 mLocationClient.startLocation(); // // mLocationClient.stopLocation();//停止定位后,本地定位服务并不会被销毁 } @Override protected void onDestroy() { super.onDestroy(); mLocationClient.onDestroy();//销毁定位客户端,同时销毁本地定位服务。 } }地址
public class HttpConfig { public static String url = "https://www.zhaoapi.cn/product/getProducts"; }
封装工具类
public class OkHttpUtils { private static OkHttpUtils okHttpUitls; private MyHandler myHandler = new MyHandler(); private OkLoadListener okLoadListener; public static OkHttpUtils getOkHttpUitls() { if (okHttpUitls == null) { okHttpUitls = new OkHttpUtils(); } return okHttpUitls; } //get public void okGet(String url) { OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyIntercepter()).build(); Request request = new Request.Builder().url(url).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Message message = myHandler.obtainMessage(); message.what = 0; message.obj = e.getMessage(); myHandler.sendMessage(message); } @Override public void onResponse(Call call, Response response) throws IOException { Message message = myHandler.obtainMessage(); message.what = 1; message.obj = response.body().string(); myHandler.sendMessage(message); } }); } //post //拦截器 class MyIntercepter implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { //使用拦截器,添加公共参数 Request request = chain.request(); String method = request.method(); if (method.equals("GET")) { String s = request.url().toString(); s = s + "&source=android"; Request newRequest = request.newBuilder().url(s).build(); Response response = chain.proceed(newRequest); return response; } else if (method.equals("POST")) { RequestBody body = request.body(); if (body instanceof FormBody) { FormBody.Builder builder = new FormBody.Builder(); for (int i = 0; i < ((FormBody) body).size(); i++) { String name = ((FormBody) body).name(i); String value = ((FormBody) body).value(i); builder.add(name, value); } builder.add("source", "android"); FormBody formBody = builder.build(); Request request1 = request.newBuilder().post(formBody).build(); Response response = chain.proceed(request1); return response; } } return null; } } //handler class MyHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: //失败 String error = (String) msg.obj; okLoadListener.okLoadError(error); break; case 1: //成功 String json = (String) msg.obj; okLoadListener.okLoadSuccess(json); break; } } } //提供外部可以访问的方法,用来设置监听的 public void setOkLoadListener(OkLoadListener okLoadListener) { this.okLoadListener = okLoadListener; } }工具类监听
public interface OkLoadListener { void okLoadSuccess(String json); void okLoadError(String error); }