充电桩app代码

ShowBuildLocationActivity.java 16KB

    package com.electric.chargingpile.activity; import android.app.Activity; import android.content.Intent; import android.graphics.Point; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.View; 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.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.BitmapDescriptor; 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.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.MyLocationStyle; import com.amap.api.services.geocoder.GeocodeSearch; import com.electric.chargingpile.R; import com.electric.chargingpile.application.MainApplication; import com.electric.chargingpile.data.BuildZhan; import com.electric.chargingpile.util.JsonUtils; import com.electric.chargingpile.util.Util; import com.umeng.analytics.MobclickAgent; import com.zhy.http.okhttp.OkHttpUtils; import com.zhy.http.okhttp.callback.StringCallback; import java.util.ArrayList; import java.util.List; import okhttp3.Call; public class ShowBuildLocationActivity extends Activity implements AMap.OnCameraChangeListener, LocationSource, AMap.OnMapLoadedListener, AMap.InfoWindowAdapter, AMapLocationListener, View.OnClickListener { private MapView mapView; private AMap aMap; private UiSettings mUiSettings; private AMapLocationClient mlocationClient; private LocationSource.OnLocationChangedListener mListener; private AMapLocationClientOption mLocationOption; private GeocodeSearch geocoderSearch; boolean isFirstLoc = true; // 是否首次定位 public static LatLng llposition = null; private String gd_jing, gd_wei, poi_jing, poi_wei; public static LatLng center = null; private Marker mMarker; private BitmapDescriptor bdLocation1 = BitmapDescriptorFactory .fromResource(R.drawable.icon_bblue); private BitmapDescriptor pointzhan = BitmapDescriptorFactory .fromResource(R.drawable.icon_bred); private ImageView iv_close; private TextView tv_tips; private TextView tv; private List<BuildZhan> list = new ArrayList<BuildZhan>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_build_location); mapView = (MapView) findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); initData(); initView(); initMap(); } private void initData() { poi_jing = getIntent().getStringExtra("poi_jing"); poi_wei = getIntent().getStringExtra("poi_wei"); } private void initView() { iv_close = (ImageView) findViewById(R.id.iv_close); iv_close.setOnClickListener(this); } private void initMap() { if (aMap == null) { aMap = mapView.getMap(); Util.setMapCustomStyleFile(aMap, getApplicationContext()); MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationIcon(com.amap.api.maps.model.BitmapDescriptorFactory .fromResource(R.drawable.point_icon));// 设置小蓝点的图标 aMap.setMyLocationStyle(myLocationStyle); mUiSettings = aMap.getUiSettings(); // geocoderSearch = new GeocodeSearch(this); // geocoderSearch.setOnGeocodeSearchListener(this); setUpMap(); } } private void setUpMap() { aMap.setOnCameraChangeListener(this); aMap.setLocationSource(this);// 设置定位监听 aMap.setOnMapLoadedListener(this); aMap.setInfoWindowAdapter(this); aMap.getUiSettings().setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示 aMap.getUiSettings().setZoomControlsEnabled(false);// 设置默认缩放按钮是否显示 aMap.getUiSettings().setScaleControlsEnabled(true);// 设置比例尺是否显示 aMap.getUiSettings().setRotateGesturesEnabled(false);// 设置地图是否旋转 aMap.getUiSettings().setAllGesturesEnabled(false); } @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 onDestroy() { mapView.onDestroy(); super.onDestroy(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } @Override public void onCameraChange(CameraPosition cameraPosition) { } @Override public void onCameraChangeFinish(CameraPosition cameraPosition) { } @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); //设置是否强制刷新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(12)); // getGaode(Double.parseDouble(poi_jing),Double.parseDouble(poi_wei)); LatLng ll = new LatLng(Double.parseDouble(poi_wei), Double.parseDouble(poi_jing)); // LatLng lll = new LatLng() // MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); // mBaiduMap.animateMapStatus(u); llposition = ll; //设置缩放级别 aMap.moveCamera(CameraUpdateFactory.zoomTo(12)); //将地图移动到定位点 aMap.moveCamera(CameraUpdateFactory.changeLatLng(llposition)); initOverlay(); } public void initOverlay() { aMap.clear(); MarkerOptions oobd = new MarkerOptions().position(llposition) .icon(bdLocation1).zIndex(9).draggable(true).title("您选择的位置").period(30); mMarker = (Marker) (aMap.addMarker(oobd)); mMarker.showInfoWindow(); showMapData(); } private void initOverlay(final List<BuildZhan> l) { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < l.size(); i++) { String poi_jing = l.get(i).getPoi_jing(); String poi_wei = l.get(i).getPoi_wei(); getGaode(Double.parseDouble(poi_jing), Double.parseDouble(poi_wei)); LatLng ll = new LatLng(Double.parseDouble(gd_wei), Double.parseDouble(gd_jing)); MarkerOptions oogift = new MarkerOptions().position(ll).icon(pointzhan) .anchor(0.5f, 1.0f).zIndex(3); Marker mMarkerGift = aMap.addMarker(oogift); } } }).start(); } private void showMapData() { new Thread(new Runnable() { @Override public void run() { Point p1 = new Point(0, 0); LatLng locLeftTop = mapView.getMap().getProjection().fromScreenLocation(p1); Point p2 = new Point(MainApplication.mScreenWidth, mapView .getHeight()); LatLng llRightBottom = mapView.getMap().getProjection() .fromScreenLocation(p2); String url = MainApplication.url + "/zhannew/basic/web/index.php/zhuanrdbz/getlist2?poi_lux=" + locLeftTop.longitude + "" + "&poi_luy=" + locLeftTop.latitude + "" + "&poi_rdx=" + llRightBottom.longitude + "" + "&poi_rdy=" + llRightBottom.latitude; OkHttpUtils.get() .url(url) .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e) { } @Override public void onResponse(String response) { Log.e("response===", response); List<BuildZhan> mZhan = JsonUtils.parseToObjectList(response, BuildZhan.class); for (int i = 0; i < mZhan.size(); i++) { BuildZhan aZhan = mZhan.get(i); } list.clear(); if (mZhan != null && mZhan.size() > 0) { list.addAll(mZhan); } initOverlay(list); } }); } }).start(); } // private void MainMap() { // try { // new Thread(new Runnable() { // // // Point p1 = new Point(0, 0); // LatLng locLeftTop = mBaiduMap.getProjection() // .fromScreenLocation(p1); // Point p2 = new Point(MainApplication.mScreenWidth, mMapView // .getHeight()); // LatLng llRightBottom = mBaiduMap.getProjection() // .fromScreenLocation(p2); // // // @Override // public void run() { // String url = MainApplication.url+"/zhannew/basic/web/index.php/zhuanrdbz/getlist2?poi_lux=" + locLeftTop.longitude + "" + "&poi_luy=" + locLeftTop.latitude + "" + "&poi_rdx=" + llRightBottom.longitude + "" + "&poi_rdy=" + llRightBottom.latitude; // submitShowMap(url); // } // }).start(); // } catch (Exception e) { // e.printStackTrace(); // new Thread(new Runnable() { // // // Point p1 = new Point(0, 0); // LatLng locLeftTop = mBaiduMap.getProjection() // .fromScreenLocation(p1); // Point p2 = new Point(MainApplication.mScreenWidth, hight); // LatLng llRightBottom = mBaiduMap.getProjection() // .fromScreenLocation(p2); // // // @Override // public void run() { // String url = MainApplication.url+"/zhannew/basic/web/index.php/zhuanrdbz/getlist2?poi_lux=" + locLeftTop.longitude + "" + "&poi_luy=" + locLeftTop.latitude + "" + "&poi_rdx=" + llRightBottom.longitude + "" + "&poi_rdy=" + llRightBottom.latitude; // submitShowMap(url); // } // }).start(); // } // // } @Override public View getInfoWindow(Marker marker) { tv = new TextView(this); tv.setBackgroundResource(R.drawable.bg_myinfowindow2_0); tv.setPadding(4, 0, 4, 16); tv.setGravity(Gravity.CENTER); if (mMarker != null) { tv.setText("我的建桩位置"); } // if (mMarker1 != null){ // tv.setText(location); // et_share_address.setText(location); // } // tv.setGravity(View.TEXT_ALIGNMENT_CENTER); tv.setTextColor(getResources().getColor(R.color.ui_65)); tv.setTextSize(15); return tv; } @Override public View getInfoContents(Marker marker) { return null; } private void getGaode(Double jing, Double wei) { // Log.e("jing", jing + ""); // Log.e("wei", 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); // Log.e("gg_lon", gg_lon + ""); gg_lat = z * Math.sin(theta); gd_wei = String.valueOf(gg_lat); // Log.e("gg_lat", gg_lat + ""); } @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null && amapLocation.getErrorCode() == 0) { 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); if (!isFirstLoc) { 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\"}"); //设置缩放级别 aMap.moveCamera(CameraUpdateFactory.zoomTo(12)); //将地图移动到定位点 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(); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_close: startActivity(new Intent(getApplication(), HotHistoryActivity.class)); finish(); break; } } }