package com.electric.chargingpile.activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.amap.api.maps.AMapUtils; import com.amap.api.maps.model.LatLng; import com.blankj.utilcode.util.LogUtils; import com.electric.chargingpile.R; import com.electric.chargingpile.data.PileData; import com.electric.chargingpile.data.RecommendZhan; import com.electric.chargingpile.util.BarColorUtil; import com.electric.chargingpile.util.JsonUtils; import com.electric.chargingpile.util.LoadingDialog; import com.electric.chargingpile.util.Util; import com.electric.chargingpile.view.LockStatusDialog; import com.umeng.analytics.MobclickAgent; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import static com.electric.chargingpile.activity.MainMapActivity.getMD5; public class RecommendChargingStationActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "RecommendChargingStationActivity"; List zhanList; private LoadingDialog loadingDialog; private View popupWindowView; private PopupWindow popupWindow; private Button cancleButton; private TextView tvOne, tvTwo, tvThree; private Double bd_lon, bd_lat, bd_jing, bd_wei; private String center_jing, center_wei, gd_jing, gd_wei, bd_jing1, bd_wei1; private LatLng center; private RecommendChargingStationActivity activity; private int condition = 1; private TextView first_condition; private TextView second_condition; private TextView third_condition; private TextView forth_condition; private RecyclerView recyclerView; private RecommendChargingStationAdapter recommendChargingStationAdapter; private TextView no_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recommend_charging_station); BarColorUtil.initStatusBarColor(RecommendChargingStationActivity.this); initView(); MobclickAgent.onEvent(getApplicationContext(), "1136"); if (zhanList.size() == 0) { loadingDialog.show(); ete("{\"fun\":\"timer\"}"); } else { filterChargingStation(); } } private void initView() { zhanList = new ArrayList<>(); activity = this; loadingDialog = new LoadingDialog(this); loadingDialog.setCanceledOnTouchOutside(false); no_result = findViewById(R.id.no_result); center_jing = getIntent().getStringExtra("center_jing"); center_wei = getIntent().getStringExtra("center_wei"); center = new LatLng(Double.parseDouble(center_wei), Double.parseDouble(center_jing)); findViewById(R.id.iv_back).setOnClickListener(this); first_condition = findViewById(R.id.first_condition); second_condition = findViewById(R.id.second_condition); third_condition = findViewById(R.id.third_condition); forth_condition = findViewById(R.id.forth_condition); first_condition.setOnClickListener(this::onClick); second_condition.setOnClickListener(this::onClick); third_condition.setOnClickListener(this::onClick); forth_condition.setOnClickListener(this::onClick); recyclerView = findViewById(R.id.recyclerView); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); recommendChargingStationAdapter = new RecommendChargingStationAdapter(); recyclerView.setAdapter(recommendChargingStationAdapter); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_back: finish(); break; case R.id.first_condition: case R.id.second_condition: case R.id.third_condition: case R.id.forth_condition: chooseCondition(v.getId()); break; } } private void chooseCondition(int id) { first_condition.setTextColor(Color.parseColor("#222222")); second_condition.setTextColor(Color.parseColor("#222222")); third_condition.setTextColor(Color.parseColor("#222222")); forth_condition.setTextColor(Color.parseColor("#222222")); switch (id) { case R.id.first_condition: condition = 1; first_condition.setTextColor(Color.parseColor("#1BB637")); MobclickAgent.onEvent(getApplicationContext(), "1139"); break; case R.id.second_condition: condition = 2; second_condition.setTextColor(Color.parseColor("#1BB637")); MobclickAgent.onEvent(getApplicationContext(), "1138"); break; case R.id.third_condition: condition = 3; third_condition.setTextColor(Color.parseColor("#1BB637")); MobclickAgent.onEvent(getApplicationContext(), "1140"); break; case R.id.forth_condition: condition = 4; forth_condition.setTextColor(Color.parseColor("#1BB637")); MobclickAgent.onEvent(getApplicationContext(), "1137"); break; } if (zhanList.size() == 0 && socket != null && socket.isClosed()) { loadingDialog.show(); ete("{\"fun\":\"timer\"}"); } else { filterChargingStation(); } } public class RecommendChargingStationAdapter extends RecyclerView.Adapter { private List mList; class ViewHolder extends RecyclerView.ViewHolder { final LinearLayout station_item; final TextView zhan_name; final TextView distance; final ImageView red_pager_bag_icon; final Button recommend_nav; final ImageView score_img; final TextView score_text; final TextView charge_record_cnt; final TextView comment_cnt; final View times_line; final LinearLayout zongjia_container; final TextView zongjia; final TextView iv_label_public; final TextView iv_label_ground; final TextView iv_label_free_park; final TextView payment_method; final TextView window_tv_fast_free_label; final TextView window_tv_fast_free_num; final TextView window_tv_fast_total_num; final TextView window_tv_slow_free_num; final TextView window_tv_slow_free_label; final TextView window_tv_slow_total_num; final TextView fenshi_info_textview; final TextView parkingFee; final TextView tvLock; public ViewHolder(View itemView) { super(itemView); station_item = itemView.findViewById(R.id.station_item); zhan_name = itemView.findViewById(R.id.zhan_name); distance = itemView.findViewById(R.id.distance); red_pager_bag_icon = itemView.findViewById(R.id.red_paper_bag); recommend_nav = itemView.findViewById(R.id.recommend_nav); score_img = itemView.findViewById(R.id.score_img); score_text = itemView.findViewById(R.id.score_text); charge_record_cnt = itemView.findViewById(R.id.charge_record_cnt); comment_cnt = itemView.findViewById(R.id.comment_cnt); times_line = itemView.findViewById(R.id.times_line); zongjia_container = itemView.findViewById(R.id.zongjia_container); zongjia = itemView.findViewById(R.id.zongjia); iv_label_public = itemView.findViewById(R.id.iv_label_public); iv_label_ground = itemView.findViewById(R.id.iv_label_ground); iv_label_free_park = itemView.findViewById(R.id.iv_label_free_park); payment_method = itemView.findViewById(R.id.payment_method); window_tv_fast_free_label = itemView.findViewById(R.id.window_tv_fast_free_label); window_tv_fast_free_num = itemView.findViewById(R.id.window_tv_fast_free_num); window_tv_fast_total_num = itemView.findViewById(R.id.window_tv_fast_total_num); window_tv_slow_free_label = itemView.findViewById(R.id.window_tv_slow_free_label); window_tv_slow_free_num = itemView.findViewById(R.id.window_tv_slow_free_num); window_tv_slow_total_num = itemView.findViewById(R.id.window_tv_slow_total_num); fenshi_info_textview = itemView.findViewById(R.id.fenshi_info_textview); parkingFee = itemView.findViewById(R.id.parkingFee); tvLock = itemView.findViewById(R.id.tvLock); } } public RecommendChargingStationAdapter() { mList = new ArrayList(); } public void setData(List list) { mList = list; notifyDataSetChanged(); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recommend_charging_station_item, parent, false); ViewHolder viewHolder = new ViewHolder(itemView); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { RecommendZhan recommendZhan = mList.get(position); holder.station_item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplication(), NewZhanDetailsActivity.class); intent.putExtra("zhan_id", recommendZhan.getZhan_id()); startActivity(intent); } }); holder.zhan_name.setText(recommendZhan.getZhan_name()); if ("0m".equals(recommendZhan.getDistance())) { holder.distance.setText("未知"); } else { holder.distance.setText(recommendZhan.getDistance()); } if ("1".equals(recommendZhan.getOwn_pay())) { holder.red_pager_bag_icon.setVisibility(View.VISIBLE); } else { holder.red_pager_bag_icon.setVisibility(View.GONE); } holder.red_pager_bag_icon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent_example = new Intent(getApplicationContext(), ExampleActivity.class); intent_example.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent_example); overridePendingTransition(0, 0); } }); holder.recommend_nav.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { recommendNav(recommendZhan); } }); showStarInfoView(holder, recommendZhan); if (recommendZhan.getCharge_record_cnt() == 0) { holder.charge_record_cnt.setVisibility(View.GONE); } else { holder.charge_record_cnt.setVisibility(View.VISIBLE); if (recommendZhan.getCharge_record_cnt() < 1000) { holder.charge_record_cnt.setText(recommendZhan.getCharge_record_cnt() + "次充电成功"); } else if (recommendZhan.getCharge_record_cnt() < 10000) { holder.charge_record_cnt.setText(recommendZhan.getCharge_record_cnt() / 1000 + "k+次充电成功"); } else { holder.charge_record_cnt.setText(recommendZhan.getCharge_record_cnt() / 10000 + "w+次充电成功"); } } if (recommendZhan.getComment_cnt() == 0) { holder.comment_cnt.setVisibility(View.GONE); } else { holder.comment_cnt.setVisibility(View.VISIBLE); if (recommendZhan.getComment_cnt() < 1000) { holder.comment_cnt.setText(recommendZhan.getComment_cnt() + "次评论"); } else if (recommendZhan.getComment_cnt() < 10000) { holder.comment_cnt.setText(recommendZhan.getComment_cnt() / 1000 + "k+次评论"); } else { holder.comment_cnt.setText(recommendZhan.getComment_cnt() / 10000 + "w+次评论"); } } if (recommendZhan.getComment_cnt() == 0 || recommendZhan.getCharge_record_cnt() == 0 && recommendZhan.getComment_cnt() == 0) { holder.times_line.setVisibility(View.GONE); } else { holder.times_line.setVisibility(View.VISIBLE); } if (recommendZhan.getZongjia() == 0) { holder.zongjia_container.setVisibility(View.GONE); } else { holder.zongjia_container.setVisibility(View.VISIBLE); holder.zongjia.setText(String.format("%.2f", recommendZhan.getZongjia())); } holder.iv_label_public.setText(recommendZhan.getBelong_attribute()); holder.iv_label_ground.setText("0".equals(recommendZhan.getPark_location()) ? "地下" : "地上"); holder.iv_label_free_park.setVisibility(recommendZhan.getStop_cost().contains("免费") ? View.VISIBLE : View.GONE); holder.parkingFee.setText("停车费:"+recommendZhan.getStop_cost()); holder.parkingFee.setVisibility(recommendZhan.getStop_cost().contains("免费") ? View.GONE : View.VISIBLE); if ("1".equals(recommendZhan.getOwn_pay())) { holder.payment_method.setText("可使用本APP扫码支付"); } else { if ("".equals(recommendZhan.getCharge_cost_way2())) { holder.payment_method.setText("暂无"); } else { holder.payment_method.setText(recommendZhan.getCharge_cost_way2()); } } if (recommendZhan.getAcableNum() !=null ){ holder.window_tv_fast_free_label.setVisibility(View.VISIBLE); holder.window_tv_fast_total_num.setVisibility(View.VISIBLE); holder.window_tv_slow_free_label.setVisibility(View.VISIBLE); holder.window_tv_slow_total_num.setVisibility(View.VISIBLE); holder.window_tv_fast_free_num.setText(recommendZhan.getDcableNum()); holder.window_tv_slow_free_num.setText(recommendZhan.getAcableNum()); holder.window_tv_fast_total_num.setText("".equals(recommendZhan.getDcNum()) ? "0" : "/" + recommendZhan.getDcNum()); holder.window_tv_slow_total_num.setText("".equals(recommendZhan.getAcNum()) ? "0" : "/" + recommendZhan.getAcNum()); }else{ if (recommendZhan.getFast_able_num() == null && recommendZhan.getSlow_able_num() == null) { holder.window_tv_fast_free_label.setVisibility(View.GONE); holder.window_tv_fast_total_num.setVisibility(View.GONE); holder.window_tv_slow_free_label.setVisibility(View.GONE); holder.window_tv_slow_total_num.setVisibility(View.GONE); holder.window_tv_fast_free_num.setText("".equals(recommendZhan.getFast_num()) ? "0" : recommendZhan.getFast_num()); holder.window_tv_slow_free_num.setText("".equals(recommendZhan.getSlow_num()) ? "0" : recommendZhan.getSlow_num()); } else { holder.window_tv_fast_free_label.setVisibility(View.VISIBLE); holder.window_tv_fast_total_num.setVisibility(View.VISIBLE); holder.window_tv_slow_free_label.setVisibility(View.VISIBLE); holder.window_tv_slow_total_num.setVisibility(View.VISIBLE); holder.window_tv_fast_free_num.setText(recommendZhan.getFast_able_num()); holder.window_tv_slow_free_num.setText(recommendZhan.getSlow_able_num()); holder.window_tv_fast_total_num.setText("".equals(recommendZhan.getFast_num()) ? "0" : "/" + recommendZhan.getFast_num()); holder.window_tv_slow_total_num.setText("".equals(recommendZhan.getSlow_num()) ? "0" : "/" + recommendZhan.getSlow_num()); } } holder.fenshi_info_textview.setText(""); if ("1".equals(recommendZhan.getFenshi_is())) { ArrayList fenshiList = (ArrayList) JsonUtils.parseToObjectList(recommendZhan.getFenshi_info(), PileData.FenshiInfoBean.class); if (fenshiList.size() == 1) { holder.fenshi_info_textview.setText("全天价格统一"); } else { Calendar calendar = Calendar.getInstance(); int hours = calendar.get(Calendar.HOUR_OF_DAY); int minutes = calendar.get(Calendar.MINUTE); int totalMinutes = hours * 60 + minutes; for (PileData.FenshiInfoBean bean : fenshiList) { if (bean.getStartTotalMinutes() > totalMinutes) { double service_free = 0; double charge_free = 0; try { service_free = Double.valueOf(bean.getService_free()); charge_free = Double.valueOf(bean.getCharge_free()); } catch (Exception e) { e.printStackTrace(); } holder.fenshi_info_textview.setText(String.format("%s开始 %.2f元/度", bean.getStart(), service_free + charge_free)); break; } } } } else { if (recommendZhan.getZongjia() != 0) { holder.fenshi_info_textview.setText("全天价格统一"); } } } @Override public int getItemCount() { return mList.size(); } private void showStarInfoView(ViewHolder holder, RecommendZhan recommendZhan) { String starStr = recommendZhan.getStar(); double star = 0.0; try { star = Double.parseDouble(starStr); } catch (NumberFormatException e) { e.printStackTrace(); } holder.score_text.setText(String.format("%.1f分", star)); if (star == 0) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_0xing)); holder.score_text.setText("暂无评分"); } else if (star > 0 && star < 1) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_05xing)); } else if (star >= 1 && star < 1.5) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_1xing)); } else if (star >= 1.5 && star < 2) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_15xing)); } else if (star >= 2 && star < 2.5) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_2xing)); } else if (star >= 2.5 && star < 3) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_25xing)); } else if (star >= 3 && star < 3.5) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_3xing)); } else if (star >= 3.5 && star < 4) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_35xing)); } else if (star >= 4 && star < 4.5) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_4xing)); } else if (star >= 4.5 && star < 5) { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_45xing)); } else { holder.score_img.setImageDrawable(getDrawable(R.drawable.icon_5xing)); } } private void recommendNav(RecommendZhan rz) { String wei = rz.getPoi_wei().trim(); String jing = rz.getPoi_jing().trim(); int position_jing = jing.length() - jing.indexOf(".") - 1; int position_wei = wei.length() - wei.indexOf(".") - 1; final LatLng ll; if (position_jing > 13 || position_wei > 13) { ll = new LatLng(Double.parseDouble(wei), Double.parseDouble(jing)); } else { double dwei = Double.parseDouble(wei); double djing = Double.parseDouble(jing); getGaode(djing, dwei); ll = new LatLng(Double.parseDouble(gd_wei), Double.parseDouble(gd_jing)); } if (!isAvilible(getApplicationContext(), "com.baidu.BaiduMap") && !isAvilible(getApplicationContext(), "com.autonavi.minimap")) { Intent intent = new Intent(getApplication(), BasicNaviActivity.class); intent.putExtra("start_jing", center.longitude + ""); intent.putExtra("start_wei", center.latitude + ""); intent.putExtra("stop_jing", ll.longitude + ""); intent.putExtra("stop_wei", ll.latitude + ""); startActivity(intent); } else { if (center != null) { LayoutInflater inflater1 = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); popupWindowView = inflater1.inflate(R.layout.item_selectmap, null); popupWindow = new PopupWindow(popupWindowView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 设置PopupWindow的弹出和消失效果 popupWindow.setAnimationStyle(R.style.popupAnimation); LinearLayout ll_tvTwo = (LinearLayout) popupWindowView.findViewById(R.id.ll_tvTwo); if (!isAvilible(getApplicationContext(), "com.autonavi.minimap")) { ll_tvTwo.setVisibility(View.GONE); } LinearLayout ll_tvOne = (LinearLayout) popupWindowView.findViewById(R.id.ll_tvOne); if (!isAvilible(getApplicationContext(), "com.baidu.BaiduMap")) { ll_tvOne.setVisibility(View.GONE); } cancleButton = (Button) popupWindowView .findViewById(R.id.cancleButton); cancleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); } }); tvThree = (TextView) popupWindowView.findViewById(R.id.tvThree); tvThree.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplication(), BasicNaviActivity.class); intent.putExtra("start_jing", center.longitude + ""); intent.putExtra("start_wei", center.latitude + ""); intent.putExtra("stop_jing", ll.longitude + ""); intent.putExtra("stop_wei", ll.latitude + ""); startActivity(intent); popupWindow.dismiss(); } }); tvOne = (TextView) popupWindowView.findViewById(R.id.tvOne); tvOne.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = null; int position_jing = rz.getPoi_jing().length() - rz.getPoi_jing().indexOf(".") - 1; int position_wei = rz.getPoi_wei().length() - rz.getPoi_wei().indexOf(".") - 1; if (position_jing > 13 || position_wei > 13) { getBaidu1(Double.parseDouble(rz.getPoi_jing()), Double.parseDouble(rz.getPoi_wei())); } else { bd_jing = Double.parseDouble(rz.getPoi_jing()); bd_wei = Double.parseDouble(rz.getPoi_wei()); } getBaidu(center.longitude, center.latitude); try { String s = "intent://map/direction?" + "origin=" + bd_lat + "," + bd_lon + "&" + "destination=" + bd_wei + "," + bd_jing + "&mode=driving&" + "src=充电桩#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end"; intent = Intent.getIntent(s); } catch (URISyntaxException e) { e.printStackTrace(); } startActivity(intent); popupWindow.dismiss(); } }); tvTwo = (TextView) popupWindowView.findViewById(R.id.tvTwo); tvTwo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { Intent intent4 = new Intent("android.intent.action.VIEW", android.net.Uri.parse("androidamap://route?sourceApplication=amap&slat=" + center.latitude + "&slon=" + center.longitude + "&sname=我的位置&dlat=" + ll.latitude + "&dlon=" + ll.longitude + "&dname=" + rz.getZhan_name() + "&dev=0&m=0&t=2")); intent4.setPackage("com.autonavi.minimap"); startActivity(intent4); popupWindow.dismiss(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplication(), "请您确认是否安装高德地图APP", Toast.LENGTH_SHORT).show(); } } }); popupWindow.showAtLocation(cancleButton, Gravity.CENTER, 0, 0); } else { Toast.makeText(getApplication(), "没有定位到您的当前位置", Toast.LENGTH_SHORT) .show(); } } } private boolean isAvilible(Context context, String packageName) { //获取packagemanager final PackageManager packageManager = context.getPackageManager(); //获取所有已安装程序的包信息 List packageInfos = packageManager.getInstalledPackages(0); //用于存储所有已安装程序的包名 List packageNames = new ArrayList(); //从pinfo中将包名字逐一取出,压入pName list中 if (packageInfos != null) { for (int i = 0; i < packageInfos.size(); i++) { String packName = packageInfos.get(i).packageName; packageNames.add(packName); } } //判断packageNames中是否有目标程序的包名,有TRUE,没有FALSE return packageNames.contains(packageName); } private void getBaidu(Double jing, Double wei) { double x_pi = 3.14159265358979324 * 3000.0 / 180.0; double x = jing; double y = wei; double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); bd_lon = z * Math.cos(theta) + 0.0065; bd_lat = z * Math.sin(theta) + 0.006; } private void getBaidu1(Double jing, Double wei) { double x_pi = 3.14159265358979324 * 3000.0 / 180.0; double x = jing; double y = wei; double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); bd_jing = z * Math.cos(theta) + 0.0065; bd_wei = z * Math.sin(theta) + 0.006; } } private void filterChargingStation() { no_result.setVisibility(View.GONE); List list = new ArrayList(); if (condition == 2) { // 价格最低 List priceList1 = new ArrayList<>(); List priceList2 = new ArrayList<>(); for (RecommendZhan point : zhanList) { if (point.getZongjia() > 0) { priceList1.add(point); } else { priceList2.add(point); } } list.addAll(priceList1); Collections.sort(list, new Comparator() { @Override public int compare(RecommendZhan o1, RecommendZhan o2) { double diff = o1.getZongjia() - o2.getZongjia(); if (o1.getZongjia() == 0) { diff = -1; } if (diff > 0) { return 1; } else if (diff < 0) { return -1; } return 0; } }); list.addAll(priceList2); } if (condition == 1) { // 距离最近 list.addAll(zhanList); Collections.sort(list, new Comparator() { @Override public int compare(RecommendZhan o1, RecommendZhan o2) { double diff = o1.getDistanceNum() - o2.getDistanceNum(); if (diff > 0) { return 1; } else if (diff < 0) { return -1; } return 0; } }); } if (condition == 3) { // 空闲最多 list.addAll(zhanList); Collections.sort(list, new Comparator() { @Override public int compare(RecommendZhan o1, RecommendZhan o2) { int fast_able_num1; int fast_able_num2; int slow_able_num1; int slow_able_num2; int total_num1; int total_num2; if (o1.getFast_able_num() == null || "".equals(o1.getFast_able_num())) { fast_able_num1 = 0; } else { fast_able_num1 = Integer.parseInt(o1.getFast_able_num()); } if (o1.getSlow_able_num() == null || "".equals(o1.getSlow_able_num())) { slow_able_num1 = 0; } else { slow_able_num1 = Integer.parseInt(o1.getSlow_able_num()); } if (o2.getFast_able_num() == null || "".equals(o2.getFast_able_num())) { fast_able_num2 = 0; } else { fast_able_num2 = Integer.parseInt(o2.getFast_able_num()); } if (o2.getSlow_able_num() == null || "".equals(o2.getSlow_able_num())) { slow_able_num2 = 0; } else { slow_able_num2 = Integer.parseInt(o2.getSlow_able_num()); } total_num1 = fast_able_num1 + slow_able_num1; total_num2 = fast_able_num2 + slow_able_num2; int diff = total_num1 - total_num2; if (diff > 0) { return -1; } else if (diff < 0) { return 1; } return 0; } }); } if (condition == 4) { // 评分最高 list.addAll(zhanList); Collections.sort(list, new Comparator() { @Override public int compare(RecommendZhan o1, RecommendZhan o2) { double star1; double star2; if (o1.getStar() == null || "".equals(o1.getStar())) { star1 = 0; } else { star1 = Double.parseDouble(o1.getStar()); } if (o2.getStar() == null || "".equals(o2.getStar())) { star2 = 0; } else { star2 = Double.parseDouble(o2.getStar()); } double diff = star1 - star2; if (diff > 0) { return -1; } else if (diff < 0) { return 1; } return 0; } }); } if (list.size() > 30) { list = list.subList(0, 30); } recommendChargingStationAdapter.setData(list); if (list.size() == 0) { if (socket != null && !socket.isClosed()) { no_result.setText("数据加载中,请稍后..."); } else { no_result.setText("周边没有符合筛选条件的站点,请检查您的筛选项"); } no_result.setVisibility(View.VISIBLE); } else { no_result.setVisibility(View.GONE); } } private void getGaode(Double jing, Double wei) { double x_pi = 3.14159265358979324 * 3000.0 / 180.0; double gg_lon; double gg_lat; double x = jing - 0.0065, y = wei - 0.006; double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi); double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi); gg_lon = z * Math.cos(theta); gd_jing = String.valueOf(gg_lon); gg_lat = z * Math.sin(theta); gd_wei = String.valueOf(gg_lat); } private Socket socket; private Handler hand = new Handler() { public void handleMessage(android.os.Message msg) { if (activity != null && !activity.isFinishing()) { loadingDialog.dismiss(); } switch (msg.what) { case 3: try { String rtnCode = JsonUtils.getKeyResult(msg.obj.toString(), "rtnCode"); if ("0".equals(rtnCode)) { try { String data = JsonUtils.getKeyResult(msg.obj.toString(), "data"); List list = JsonUtils.parseToObjectList(data, RecommendZhan.class); if (list != null) { int count = list.size(); for (int i = 0; i < count; i++) { String wei = list.get(i).getPoi_wei().trim(); String jing = list.get(i).getPoi_jing().trim(); int position_jing = jing.length() - jing.indexOf(".") - 1; int position_wei = wei.length() - wei.indexOf(".") - 1; LatLng ll; if (position_jing > 13 || position_wei > 13) { ll = new LatLng(Double.parseDouble(wei), Double.parseDouble(jing)); } else { double dwei = Double.parseDouble(wei); double djing = Double.parseDouble(jing); getGaode(djing, dwei); ll = new LatLng(Double.parseDouble(gd_wei), Double.parseDouble(gd_jing)); } double d = AMapUtils.calculateLineDistance(center, ll); list.get(i).setDistanceNum(d); list.get(i).setDistance(Util.getDistance(d)); } zhanList = list; filterChargingStation(); } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } break; default: break; } } }; //获取站点列表 private void ete(final String params) { new Thread(new Runnable() { @Override public void run() { try { // 创建Socket socket = new Socket("cdz.evcharge.cc", 9503); // IP:59.110.68.162,端口54321 cdz.evcharge.cc // 向服务器发送消息 PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true); out.println(params); // 接收来自服务器的消息 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String msg = br.readLine(); int time = (int) (System.currentTimeMillis() / 1000); int cha = time - Integer.parseInt(msg); String Token = getMD5(String.valueOf((System.currentTimeMillis() / 1000 - cha) / 10) + "qwerty"); String llon, llat, rlon, rlat; llon = String.valueOf(Double.parseDouble(center_jing) - 0.047414454728858857); llat = String.valueOf(Double.parseDouble(center_wei) + 0.047414454728858857); rlon = String.valueOf(Double.parseDouble(center_jing) + 0.047414454728858857); rlat = String.valueOf(Double.parseDouble(center_wei) - 0.041842459916509256); HashMap map = new HashMap(); map.put("fun", "zhangetlist"); map.put("poi_lux", llon); map.put("poi_luy", llat); map.put("poi_rdx", rlon); map.put("poi_rdy", rlat); map.put("timer", Token); String socket_string = JsonUtils.mapToJson(map); ete1(socket_string); } catch (Exception e) { e.printStackTrace(); Message mes = new Message(); mes.obj = "error"; mes.what = 100; hand.sendMessage(mes); } } }).start(); } private void ete1(final String params) { new Thread(new Runnable() { @Override public void run() { try { socket = new Socket("cdz.evcharge.cc", 9503); // IP:59.110.68.162,端口54321 cdz.evcharge.cc socket.setReceiveBufferSize(99999); // 接收缓冲大小 socket.setSendBufferSize(99999); // 向服务器发送消息 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); out.println(params); // 接收来自服务器的消息 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String msg = br.readLine(); if (msg != null) { Message mes = new Message(); mes.obj = msg; mes.what = 3; hand.sendMessage(mes); } else { LogUtils.e("msg=null"); } out.close(); br.close(); // 关闭Socket socket.close(); } catch (Exception e) { e.printStackTrace(); Message mes = new Message(); mes.obj = "error"; mes.what = 100; hand.sendMessage(mes); } } }).start(); } }