导航菜单

页面标题

页面副标题

爱心e站 v1.0.0 - BaiduMapGuildActivity.java 源代码

正在查看: 爱心e站 v1.0.0 应用的 BaiduMapGuildActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.example.baidumap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.asm.Opcodes;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.route.BikingRouteLine;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteLine;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.example.baidumap.mapUtils.LocationService;
import com.example.baidumap.mapUtils.MapUtil;
import com.example.baidumap.mapUtils.TransitRouteOverlay;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaiduMapGuildActivity extends Activity {
    private boolean addressLocation;
    private ImageView back;
    public ProgressDialog dialog;
    private EditText endLocation;
    private GeoCoder geoCoder;
    private BDLocation lastLocation;
    private double latitude;
    private TextView line1;
    private TextView line2;
    private TextView line3;
    private RadioGroup lineType;
    private ImageView locationME;
    private ImageView locationORg;
    private LocationService locationService;
    private double longitude;
    private BaiduMap mBaiduMap;
    private MapView mMapView;
    private RoutePlanSearch mRoutePlanSearch;
    private Spinner mySpinner;
    private TransitRouteOverlay overlay;
    private List<TransitRouteLine> routeLines;
    private EditText startLocation;
    private TransitRouteLine transitRouteLine;
    private String city = "济南";
    private String address = "";
    private List<String> busList = new ArrayList();

    @SuppressLint({"HandlerLeak"})
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (BaiduMapGuildActivity.this.geoCoder == null) {
                        BaiduMapGuildActivity.this.startLocation.setText(BaiduMapGuildActivity.this.lastLocation.getAddrStr());
                        BaiduMapGuildActivity.this.myLocation();
                        break;
                    }
                    break;
                case 2:
                    BaiduMapGuildActivity.this.showToast("暂无路线方案,请尝试其他出行方案!");
                    break;
                case 3:
                    ((RadioButton) BaiduMapGuildActivity.this.findViewById(R.id.rb_busline)).setChecked(false);
                    break;
                case 4:
                    Toast.makeText(BaiduMapGuildActivity.this, "位置服务失败,请稍后重试", 0).show();
                    BaiduMapGuildActivity.this.finish();
                    break;
            }
        }
    };
    private BDLocationListener mListener = new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (BaiduMapGuildActivity.this.lastLocation != location) {
                if (location != null && location.getLocType() != 167) {
                    MapUtil.addmark(BaiduMapGuildActivity.this.mBaiduMap, Double.valueOf(location.getLatitude()), Double.valueOf(location.getLongitude()), false, R.drawable.icon_my_location, 3);
                    BaiduMapGuildActivity.this.lastLocation = location;
                    BaiduMapGuildActivity.this.city = BaiduMapGuildActivity.this.lastLocation.getCity();
                    BaiduMapGuildActivity.this.handler.sendEmptyMessage(1);
                    return;
                }
                BaiduMapGuildActivity.this.showToast("定位失败,请确认是否打开了定位服务!");
                BaiduMapGuildActivity.this.finish();
            }
        }

        public void onConnectHotSpotMessage(String s, int i) {
        }
    };
    OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
        @Override
        public void onGetWalkingRouteResult(WalkingRouteResult result) {
            List<WalkingRouteLine> routeLines = result.getRouteLines();
            routeLines.get(0);
        }

        @Override
        public void onGetTransitRouteResult(TransitRouteResult result) {
            BaiduMapGuildActivity.this.cancleProgress();
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                BaiduMapGuildActivity.this.handler.sendEmptyMessage(2);
            }
            if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
                BaiduMapGuildActivity.this.handler.sendEmptyMessage(2);
                return;
            }
            if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                BaiduMapGuildActivity.this.routeLines = result.getRouteLines();
                if (BaiduMapGuildActivity.this.busList != null && BaiduMapGuildActivity.this.busList.size() > 0) {
                    BaiduMapGuildActivity.this.busList.clear();
                }
                for (int i = 0; i < BaiduMapGuildActivity.this.routeLines.size(); i++) {
                    BaiduMapGuildActivity.this.busList.add("路线 " + (i + 1));
                }
                if (BaiduMapGuildActivity.this.busList.size() > 0) {
                    BaiduMapGuildActivity.this.findViewById(R.id.button).setVisibility(8);
                    BaiduMapGuildActivity.this.findViewById(R.id.spinner).setVisibility(0);
                    BaiduMapGuildActivity.this.initSpinner();
                }
                if (BaiduMapGuildActivity.this.routeLines != null && BaiduMapGuildActivity.this.routeLines.size() > 0 && BaiduMapGuildActivity.this.routeLines.get(0) != null) {
                    BaiduMapGuildActivity.this.line1.setVisibility(0);
                }
                if (BaiduMapGuildActivity.this.routeLines != null && BaiduMapGuildActivity.this.routeLines.size() > 1 && BaiduMapGuildActivity.this.routeLines.get(1) != null) {
                    BaiduMapGuildActivity.this.line2.setVisibility(0);
                }
                if (BaiduMapGuildActivity.this.routeLines != null && BaiduMapGuildActivity.this.routeLines.size() > 2 && BaiduMapGuildActivity.this.routeLines.get(2) != null) {
                    BaiduMapGuildActivity.this.line3.setVisibility(0);
                }
            }
        }

        @Override
        public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {
        }

        @Override
        public void onGetDrivingRouteResult(DrivingRouteResult result) {
        }

        @Override
        public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {
        }

        @Override
        public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
            List<BikingRouteLine> routeLines = bikingRouteResult.getRouteLines();
            if (routeLines != null) {
                routeLines.get(0);
                if (routeLines != null && routeLines.size() > 0 && routeLines.get(0) != null) {
                    BaiduMapGuildActivity.this.line1.setVisibility(0);
                }
                if (routeLines != null && routeLines.size() > 1 && routeLines.get(1) != null) {
                    BaiduMapGuildActivity.this.line2.setVisibility(0);
                }
                if (routeLines != null && routeLines.size() > 2 && routeLines.get(2) != null) {
                    BaiduMapGuildActivity.this.line3.setVisibility(0);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SDKInitializer.initialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baidu_map_guild);
        initViews();
        setListeners();
        if (this.locationService == null) {
            this.locationService = new LocationService(getApplicationContext());
        }
        initGetLocation();
        Intent intent = getIntent();
        this.address = intent.getStringExtra("address");
        try {
            String latitude = intent.getStringExtra("latitude");
            String longitude = intent.getStringExtra("longitude");
            if (latitude == null || latitude.length() == 0) {
                latitude = "0";
            }
            this.latitude = Double.valueOf(latitude).doubleValue();
            if (longitude == null || longitude.length() == 0) {
                longitude = "0";
            }
            this.longitude = Double.valueOf(longitude).doubleValue();
            if (this.latitude == 0.0d || this.longitude == 0.0d) {
                if (this.address == null || this.address.length() == 0) {
                    showToast("未获取到经纬度以及地址信息,无法进行导航");
                    finish();
                    return;
                } else {
                    this.addressLocation = true;
                    findViewById(R.id.btn_jigou_location).performClick();
                }
            } else {
                this.addressLocation = false;
                LatLng sourceLatLng = new LatLng(this.latitude, this.longitude);
                CoordinateConverter converter = new CoordinateConverter();
                converter.from(CoordinateConverter.CoordType.GPS);
                converter.coord(sourceLatLng);
                LatLng desLatLng = converter.convert();
                this.latitude = desLatLng.latitude;
                this.longitude = desLatLng.longitude;
                MapUtil.addmark(this.mBaiduMap, Double.valueOf(this.latitude), Double.valueOf(this.longitude), true, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            showToast("未获取到经纬度以及地址信息,无法进行导航");
            finish();
        }
        initData();
    }

    private void setListeners() {
        this.line1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaiduMapGuildActivity.this.showBusLine(0);
                BaiduMapGuildActivity.this.myLocationDetail();
            }
        });
        this.line2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaiduMapGuildActivity.this.showBusLine(1);
                BaiduMapGuildActivity.this.myLocationDetail();
            }
        });
        this.line3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaiduMapGuildActivity.this.showBusLine(2);
                BaiduMapGuildActivity.this.myLocationDetail();
            }
        });
        this.lineType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_bikeline) {
                    Intent intentBikeGuid = new Intent(BaiduMapGuildActivity.this, (Class<?>) BNaviMainActivity.class);
                    if (BaiduMapGuildActivity.this.lastLocation == null || BaiduMapGuildActivity.this.latitude == 0.0d || BaiduMapGuildActivity.this.longitude == 0.0d) {
                        BaiduMapGuildActivity.this.testGEO();
                        ((RadioButton) BaiduMapGuildActivity.this.findViewById(R.id.rb_bikeline)).setChecked(false);
                        BaiduMapGuildActivity.this.cancleProgress();
                        return;
                    } else {
                        LatLng startLat = new LatLng(BaiduMapGuildActivity.this.lastLocation.getLatitude(), BaiduMapGuildActivity.this.lastLocation.getLongitude());
                        LatLng endLat = new LatLng(BaiduMapGuildActivity.this.latitude, BaiduMapGuildActivity.this.longitude);
                        intentBikeGuid.putExtra("start", startLat);
                        intentBikeGuid.putExtra("end", endLat);
                        BaiduMapGuildActivity.this.startActivity(intentBikeGuid);
                        ((RadioButton) BaiduMapGuildActivity.this.findViewById(R.id.rb_bikeline)).setChecked(false);
                    }
                }
                if (checkedId == R.id.rb_busline) {
                    BaiduMapGuildActivity.this.getBusline();
                }
                if (checkedId == R.id.rb_carline) {
                }
            }
        });
        this.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaiduMapGuildActivity.this.finish();
            }
        });
        this.locationME.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaiduMapGuildActivity.this.myLocation();
            }
        });
        this.locationORg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaiduMapGuildActivity.this.testGEO();
            }
        });
    }

    private void initData() {
        this.endLocation.setText(this.address == null ? "" : this.address);
        if (this.lastLocation != null) {
            this.startLocation.setText(this.lastLocation.getAddrStr());
        }
    }

    private void initGetLocation() {
        this.locationService.registerListener(this.mListener);
        this.locationService.setLocationOption(this.locationService.getDefaultLocationClientOption());
    }

    public void initViews() {
        this.mMapView = (MapView) findViewById(R.id.mv_map);
        this.startLocation = (EditText) findViewById(R.id.tv_startlocation);
        this.endLocation = (EditText) findViewById(R.id.tv_endlocation);
        this.line1 = (TextView) findViewById(R.id.tv_line1);
        this.line2 = (TextView) findViewById(R.id.tv_line2);
        this.line3 = (TextView) findViewById(R.id.tv_line3);
        this.lineType = (RadioGroup) findViewById(R.id.rg_linetype);
        this.back = (ImageView) findViewById(R.id.iv_back);
        this.locationME = (ImageView) findViewById(R.id.iv_location_me);
        this.locationORg = (ImageView) findViewById(R.id.iv_locaiton_org);
        this.mBaiduMap = this.mMapView.getMap();
        this.mBaiduMap.setMapType(1);
        this.mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15.0f));
        this.mBaiduMap.setMyLocationEnabled(true);
        this.mBaiduMap.setMyLocationEnabled(true);
        this.mMapView.getChildAt(2).setPadding(0, 0, 0, Opcodes.IF_ICMPNE);
        this.mMapView.getChildAt(1).setPadding(0, 0, 100, 100);
        this.startLocation.setEnabled(false);
        this.endLocation.setEnabled(false);
    }

    public boolean checkChinese(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Pattern p = Pattern.compile("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$");
        Matcher m = p.matcher(str);
        return m.matches();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.locationService.unregisterListener(this.mListener);
        if (this.geoCoder != null) {
            this.geoCoder.destroy();
        }
        if (this.mRoutePlanSearch != null) {
            this.mRoutePlanSearch.destroy();
        }
        this.mMapView.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.mMapView.onResume();
        this.locationService.stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.mMapView.onResume();
        this.locationService.start();
    }

    public void geoCode(String city, String address) {
        showProgress("正在定位机构地址", true);
        if (this.geoCoder == null) {
            this.geoCoder = GeoCoder.newInstance();
        }
        OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult result) {
                if (result != null && result.error == SearchResult.ERRORNO.NO_ERROR) {
                    MapUtil.addmark(BaiduMapGuildActivity.this.mBaiduMap, Double.valueOf(result.getLocation().latitude), Double.valueOf(result.getLocation().longitude), true, 0, 0);
                    BaiduMapGuildActivity.this.latitude = result.getLocation().latitude;
                    BaiduMapGuildActivity.this.longitude = result.getLocation().longitude;
                    BaiduMapGuildActivity.this.cancleProgress();
                    return;
                }
                BaiduMapGuildActivity.this.cancleProgress();
                BaiduMapGuildActivity.this.handler.sendEmptyMessage(4);
            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
                if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                }
                BaiduMapGuildActivity.this.cancleProgress();
            }
        };
        GeoCodeOption geoCodeOption = new GeoCodeOption();
        geoCodeOption.address(address);
        geoCodeOption.city(city);
        this.geoCoder.geocode(geoCodeOption);
        this.geoCoder.setOnGetGeoCodeResultListener(listener);
    }

    public void testGEO() {
        if (this.addressLocation) {
            geoCode(this.city, this.address);
        } else {
            MapUtil.addmark(this.mBaiduMap, Double.valueOf(this.latitude), Double.valueOf(this.longitude), true, 0, 0);
        }
    }

    public void getBusline() {
        if (this.lastLocation == null) {
            this.handler.sendEmptyMessage(3);
            return;
        }
        showProgress("正在规划公交路线", true);
        PlanNode stNode = PlanNode.withLocation(new LatLng(this.lastLocation.getLatitude(), this.lastLocation.getLongitude()));
        if (this.latitude == 0.0d || this.longitude == 0.0d) {
            testGEO();
            showToast("正在解析机构经纬度信息,请稍后重试!");
            this.handler.sendEmptyMessage(3);
            cancleProgress();
            return;
        }
        PlanNode enNode = PlanNode.withLocation(new LatLng(this.latitude, this.longitude));
        getRoutePlanning(stNode, enNode, this.lastLocation.getCity());
    }

    public void myLocation() {
        if (this.lastLocation != null) {
            MapUtil.addmark(this.mBaiduMap, Double.valueOf(this.lastLocation.getLatitude()), Double.valueOf(this.lastLocation.getLongitude()), true, R.drawable.icon_my_location, 3);
        }
    }

    public void myLocationDetail() {
        if (this.transitRouteLine == null) {
            showToast("请先点击\"公交路线\"进行路线规划");
            return;
        }
        Intent intent = new Intent(this, (Class<?>) LinesShowActivity.class);
        intent.putExtra("lines", this.transitRouteLine);
        startActivity(intent);
    }

    public void getRoutePlanning(PlanNode start, PlanNode end, String city) {
        this.mRoutePlanSearch = RoutePlanSearch.newInstance();
        this.mRoutePlanSearch.setOnGetRoutePlanResultListener(this.listener);
        TransitRoutePlanOption transitRoutePlanOption = new TransitRoutePlanOption();
        transitRoutePlanOption.from(start);
        transitRoutePlanOption.city(city);
        transitRoutePlanOption.to(end);
        this.mRoutePlanSearch.transitSearch(transitRoutePlanOption);
    }

    private class MyTransitRouteOverlay extends TransitRouteOverlay {
        public MyTransitRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
        }
    }

    public void initSpinner() {
        this.mySpinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, this.busList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        this.mySpinner.setAdapter((SpinnerAdapter) adapter);
        this.mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                BaiduMapGuildActivity.this.showBusLine(arg2);
                arg0.setVisibility(0);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                arg0.setVisibility(0);
            }
        });
    }

    public void showBusLine(int postion) {
        if (this.overlay == null) {
            this.overlay = new MyTransitRouteOverlay(this.mBaiduMap);
        } else {
            this.overlay.removeFromMap();
        }
        if (this.routeLines != null && this.routeLines.size() > postion) {
            this.mBaiduMap.setOnMarkerClickListener(this.overlay);
            this.transitRouteLine = this.routeLines.get(postion);
            this.overlay.setData(this.transitRouteLine);
            this.overlay.addToMap();
            this.overlay.zoomToSpan();
        }
    }

    public void showProgress(String msg, boolean isTouchDismiss) {
        if (this.dialog == null) {
            this.dialog = new ProgressDialog(this);
        }
        this.dialog.setMessage(msg);
        this.dialog.setCanceledOnTouchOutside(isTouchDismiss);
        this.dialog.setCancelable(isTouchDismiss);
        this.dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
            }
        });
        this.dialog.show();
    }

    public void cancleProgress() {
        if (this.dialog != null && this.dialog.isShowing()) {
            this.dialog.dismiss();
            this.dialog = null;
        }
    }

    public void showToast(String str) {
        Toast.makeText(this, str, 0).show();
    }
}