充电桩app代码

ShareMapActivity.java 20KB

    package com.electric.chargingpile.activity; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.text.Selection; import android.text.Spannable; import android.util.Log; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.ImageView; import android.widget.RelativeLayout; 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.maps.AMap; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.LocationSource; import com.amap.api.maps.MapView; import com.amap.api.maps.UiSettings; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.CameraPosition; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.MyLocationStyle; import com.amap.api.services.core.LatLonPoint; 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.electric.chargingpile.R; import com.electric.chargingpile.application.MainApplication; import com.electric.chargingpile.util.BarColorUtil; import com.electric.chargingpile.util.Util; import com.umeng.analytics.MobclickAgent; public class ShareMapActivity extends Activity implements View.OnClickListener, GeocodeSearch.OnGeocodeSearchListener, AMap.OnCameraChangeListener, LocationSource, AMap.OnMapLoadedListener, AMapLocationListener { private EditText tv_addressName; private String address; private LatLng center = null; private RelativeLayout tv_notice; private ImageView inBtn, outBtn, ivRefresh, iv_close, iv_back, iv_edit; private float minZoomLevel; private float maxZoomLevel; boolean isFirstLoc = true; // 是否首次定位 private TextView tv_submit; private String poi_jing, poi_wei; private MapView mapView; private AMap aMap; private UiSettings mUiSettings; private AMapLocationClient mlocationClient; private LocationSource.OnLocationChangedListener mListener; private AMapLocationClientOption mLocationOption; private GeocodeSearch geocoderSearch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_share_map); BarColorUtil.initStatusBarColor(ShareMapActivity.this); mapView = (MapView) findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); initViews(); initMap(); } private void initViews() { tv_addressName = (EditText) findViewById(R.id.tv_addressName); tv_addressName.setEnabled(false); tv_submit = (TextView) findViewById(R.id.tv_submit); tv_submit.setOnClickListener(this); inBtn = (ImageView) findViewById(R.id.zoomin1); outBtn = (ImageView) findViewById(R.id.zoomout1); inBtn.setOnClickListener(this); outBtn.setOnClickListener(this); ivRefresh = (ImageView) findViewById(R.id.iv_point_again); ivRefresh.setOnClickListener(this); iv_close = (ImageView) findViewById(R.id.iv_close); iv_close.setOnClickListener(this); tv_notice = (RelativeLayout) findViewById(R.id.tv_notice); tv_notice.setOnClickListener(this); iv_edit = (ImageView) findViewById(R.id.iv_edit); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(tv_addressName.getWindowToken(), 0); iv_edit.setOnClickListener(this); if (MainApplication.build_flag.equals("0")) { tv_notice.setVisibility(View.VISIBLE); } else if (MainApplication.build_flag.equals("1")) { tv_notice.setVisibility(View.GONE); } iv_back = (ImageView) findViewById(R.id.iv_back); iv_back.setOnClickListener(this); } private void initMap() { if (aMap == null) { aMap = mapView.getMap(); Util.setMapCustomStyleFile(aMap, getApplicationContext()); aMap.getUiSettings().setGestureScaleByMapCenter(true); MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationIcon(BitmapDescriptorFactory .fromResource(R.drawable.point_icon));// 设置小蓝点的图标 aMap.setMyLocationStyle(myLocationStyle); mUiSettings = aMap.getUiSettings(); //获得地图的最大和最小缩放级别 maxZoomLevel = aMap.getMaxZoomLevel(); minZoomLevel = aMap.getMinZoomLevel(); geocoderSearch = new GeocodeSearch(this); geocoderSearch.setOnGeocodeSearchListener(this); setUpMap(); } } private void setUpMap() { aMap.setOnCameraChangeListener(this); aMap.setLocationSource(this);// 设置定位监听 aMap.setOnMapLoadedListener(this); aMap.getUiSettings().setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示 aMap.getUiSettings().setZoomControlsEnabled(false);// 设置默认缩放按钮是否显示 aMap.getUiSettings().setScaleControlsEnabled(true);// 设置比例尺是否显示 aMap.getUiSettings().setRotateGesturesEnabled(false);// 设置地图是否旋转 } // private void initMap(){ // // 地图初始化 // mMapView = (MapView) findViewById(R.id.bmapView); // // // //隐藏地图的自带的缩放按钮 // mMapView.showZoomControls(false); // mBaiduMap = mMapView.getMap(); // //获得地图的最大和最小缩放级别 // maxZoomLevel = mBaiduMap.getMaxZoomLevel(); // minZoomLevel = mBaiduMap.getMinZoomLevel(); // controlZoomShow(); // // 隐藏定位图层(隐藏当前位置的小蓝点) // mBaiduMap.setMyLocationEnabled(false); // // 定位初始化 // mLocClient = new LocationClient(this); // mLocClient.registerLocationListener(myListener); // // 地图UI控件设置,设置地图不可翻转 // mUiSettings = mBaiduMap.getUiSettings(); // mUiSettings.setRotateGesturesEnabled(false); // mUiSettings.setCompassEnabled(false); // LocationClientOption option = new LocationClientOption(); // option.setOpenGps(true); // 打开gps // option.setCoorType("bd09ll"); // 设置坐标类型 // option.setScanSpan(1000); // mLocClient.setLocOption(option); // mLocClient.start(); // BaiduMap.OnMapStatusChangeListener listener = new BaiduMap.OnMapStatusChangeListener() { // public void onMapStatusChangeStart(MapStatus status) { // tv_addressName.setText("正在获取位置,请您耐心等待..."); // tv_addressName.setTextColor(getResources().getColor(R.color.ui_68)); // tv_submit.setBackgroundColor(getResources().getColor(R.color.huise)); // } // // public void onMapStatusChange(MapStatus status) { // } // // public void onMapStatusChangeFinish(MapStatus status) { // LatLng target = mBaiduMap.getMapStatus().target; // getAddress(target); // } // }; // mBaiduMap.setOnMapStatusChangeListener(listener); // // } // // private void getAddress( LatLng llposition) { // poi_jing = String.valueOf(llposition.longitude); // poi_wei = String.valueOf(llposition.latitude); // mSearch = GeoCoder.newInstance(); // mSearch.setOnGetGeoCodeResultListener(this); // mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(llposition)); // } // // @Override // public void onGetGeoCodeResult(GeoCodeResult result) { // if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { // Toast.makeText(getApplication(), "抱歉,未能找到结果", Toast.LENGTH_LONG) // .show(); // return; // } // } // // @Override // public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) { // if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { // return; // } // address = result.getAddress(); // tv_addressName.setText(address); // tv_addressName.setTextColor(getResources().getColor(R.color.ui_65)); // tv_submit.setBackgroundColor(getResources().getColor(R.color.lvse)); // } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_submit: // Intent intent = new Intent(this,BuildInfoActivity.class); // intent.putExtra("address",tv_addressName.getText().toString()); // intent.putExtra("poi_jing",poi_jing); // intent.putExtra("poi_wei",poi_wei); // startActivity(intent); // finish(); address = tv_addressName.getText().toString(); SharedPreferences mSharedPreferences = getSharedPreferences("shareInfo", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putString("address", address); editor.putString("poi_jing", poi_jing); editor.putString("poi_wei", poi_wei); editor.commit(); finish(); break; case R.id.iv_close: tv_notice.setVisibility(View.GONE); // MainApplication.build_flag = "1"; break; case R.id.iv_point_again: if (!isFirstLoc) { isFirstLoc = true; isFirstLoc = true; aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false // 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种 aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE); // aMap.moveCamera(); aMap.moveCamera(CameraUpdateFactory.zoomTo(16)); mlocationClient.startLocation(); } break; case R.id.zoomin1: aMap.moveCamera(CameraUpdateFactory.zoomIn()); controlZoomShow(); break; case R.id.zoomout1: aMap.moveCamera(CameraUpdateFactory.zoomOut()); // mBaiduMap.setMapStatus(MapStatusUpdateFactory // .zoomTo(mapStatus.zoom - 1)); controlZoomShow(); break; case R.id.iv_back: finish(); // startActivity(new Intent(getApplication(),BuildHomePageActivity.class)); // overridePendingTransition(0, 0); break; case R.id.iv_edit: tv_addressName.setEnabled(true); CharSequence text = tv_addressName.getText(); if (text instanceof Spannable) { Spannable spanText = (Spannable) text; Selection.setSelection(spanText, text.length()); } InputMethodManager inputManager = (InputMethodManager) tv_addressName.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(tv_addressName, 0); break; } } // public class MyLocationListenner implements BDLocationListener { // // @Override // public void onReceiveLocation(BDLocation location) { // // map view 销毁后不在处理新接收的位置 // if (location == null || mMapView == null) { // return; // } // MyLocationData locData = new MyLocationData.Builder() // .accuracy(0) // // 此处设置开发者获取到的方向信息,顺时针0-360 // .direction(100).latitude(location.getLatitude()) // .longitude(location.getLongitude()).build(); // mBaiduMap.setMyLocationData(locData); // // if (isFirstLoc) { // isFirstLoc = false; // LatLng ll = new LatLng(location.getLatitude(), // location.getLongitude()); // MapStatus.Builder builder = new MapStatus.Builder(); // builder.target(ll).zoom(15f); // mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); // getAddress(ll); // } // } // // public void onReceivePoi(BDLocation poiLocation) { // } // } //地图缩放方法 private void controlZoomShow() { float zoom = aMap.getCameraPosition().zoom; try { if (zoom >= maxZoomLevel) { inBtn.setImageResource(R.drawable.icon_nojia2_0); inBtn.setEnabled(false); // Toast.makeText(getApplication(), "已放大到最大级别", Toast.LENGTH_SHORT) // .show(); return; } else { inBtn.setImageResource(R.drawable.icon_jia2_0); inBtn.setEnabled(true); } if (zoom <= minZoomLevel) { outBtn.setImageResource(R.drawable.icon_nojian2_0); outBtn.setEnabled(false); // Toast.makeText(getApplication(), "已缩小到最小级别", Toast.LENGTH_SHORT) // .show(); return; } else { outBtn.setImageResource(R.drawable.icon_jian2_0); outBtn.setEnabled(true); } } catch (Exception e) { e.printStackTrace(); } } @Override protected void onPause() { mapView.onPause(); super.onPause(); MobclickAgent.onPause(this); } @Override protected void onResume() { mapView.onResume(); super.onResume(); MobclickAgent.onResume(this); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); if (null != mlocationClient) { mlocationClient.onDestroy(); } } @Override public void onRegeocodeSearched(final RegeocodeResult result, int i) { if (i == 1000) { if (result != null && result.getRegeocodeAddress() != null && result.getRegeocodeAddress().getFormatAddress() != null) { Log.e("-------------", "---------------"); tv_addressName.setText(result.getRegeocodeAddress().getFormatAddress()); tv_submit.setBackgroundColor(getResources().getColor(R.color.lvse)); tv_submit.setClickable(true); } } } @Override public void onGeocodeSearched(GeocodeResult geocodeResult, int i) { } @Override public void onCameraChange(CameraPosition cameraPosition) { tv_addressName.setText("正在获取位置,请您耐心等待..."); tv_submit.setBackgroundColor(getResources().getColor(R.color.huise)); tv_submit.setClickable(false); } @Override public void onCameraChangeFinish(CameraPosition cameraPosition) { controlZoomShow(); LatLng target = cameraPosition.target; getAddress(new LatLonPoint(target.latitude, target.longitude)); poi_jing = String.valueOf(target.longitude); poi_wei = String.valueOf(target.latitude); } @Override public void activate(OnLocationChangedListener onLocationChangedListener) { mListener = onLocationChangedListener; if (mlocationClient == null) { mlocationClient = new AMapLocationClient(this); mLocationOption = new AMapLocationClientOption(); //设置定位监听 mlocationClient.setLocationListener(this); //设置为高精度定位模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位参数 mlocationClient.setLocationOption(mLocationOption); // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求 // 在定位结束后,在合适的生命周期调用onDestroy()方法 // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除 // 设置是否只定位一次,默认为false mLocationOption.setOnceLocation(true); // mLocationOption.setOnceLocationLatest(true); //设置是否强制刷新WIFI,默认为强制刷新 mLocationOption.setWifiActiveScan(false); mlocationClient.startLocation(); } } @Override public void deactivate() { mListener = null; if (mlocationClient != null) { mlocationClient.stopLocation(); mlocationClient.onDestroy(); } mlocationClient = null; } @Override public void onMapLoaded() { aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false // 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种 aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE); // aMap.moveCamera(); aMap.moveCamera(CameraUpdateFactory.zoomTo(16)); } public void getAddress(final LatLonPoint latLonPoint) { RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 geocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求 } @Override public void onLocationChanged(AMapLocation amapLocation) { if (mListener != null && amapLocation != null) { if (amapLocation != null && amapLocation.getErrorCode() == 0) { mlocationClient.stopLocation(); Log.e("------!", "-----!!!"); try { tv_addressName.setText(amapLocation.getAddress()); poi_jing = String.valueOf(amapLocation.getLongitude()); poi_wei = String.valueOf(amapLocation.getLatitude()); } catch (NullPointerException e) { e.printStackTrace(); } // ProfileManager.getInstance().setAtCity(getApplicationContext(), MainApplication.atCity); center = new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude()); // Log.e("!@#$",amapLocation.getLatitude()+"```"+amapLocation.getLongitude()); //// Toast.makeText(this, amapLocation.getAddress(), Toast.LENGTH_SHORT).show(); //// Toast.makeText(this, amapLocation.getCity(), Toast.LENGTH_SHORT).show(); // ete("{\"fun\":\"timer\"}"); if (isFirstLoc) { //设置缩放级别 aMap.moveCamera(CameraUpdateFactory.zoomTo(16)); //将地图移动到定位点 aMap.moveCamera(CameraUpdateFactory.changeLatLng(center)); //点击定位按钮 能够将地图的中心移动到定位点 mListener.onLocationChanged(amapLocation); isFirstLoc = false; } } else { String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo(); Log.e("AmapErr", errText); // mLocationErrText.setVisibility(View.VISIBLE); Toast.makeText(this, errText, Toast.LENGTH_SHORT).show(); } } } }