|
package com.electric.chargingpile.activity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
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.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
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.eventbus.PoiEvent;
import com.electric.chargingpile.util.ActivityManagerApplication;
import com.electric.chargingpile.util.BarColorUtil;
import com.electric.chargingpile.util.EventBusUtil;
import com.electric.chargingpile.util.Util;
import com.umeng.analytics.MobclickAgent;
import java.util.List;
public class RouteSelectPointActivity extends Activity implements View.OnClickListener, AMap.OnCameraChangeListener, LocationSource, AMap.OnMapLoadedListener, GeocodeSearch.OnGeocodeSearchListener, AMapLocationListener {
private LatLng center = null;
private RelativeLayout tv_notice, rl_now;
private ImageView inBtn, outBtn, ivRefresh, iv_close, iv_back;
private float minZoomLevel;
private float maxZoomLevel;
boolean isFirstLoc = true; // 是否首次定位
private TextView tv_name, tv_submit, tv_address;
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;
private POIAdapter adapter;
private List<PoiItem> pl;
private ListView list_poi;
private String tag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_select_point);
BarColorUtil.initStatusBarColor(RouteSelectPointActivity.this);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
initDatas();
initViews();
initMap();
}
private void initDatas() {
tag = getIntent().getStringExtra("tag");
// Toast.makeText(this,tag,Toast.LENGTH_SHORT).show();
}
private void initViews() {
tv_name = (TextView) findViewById(R.id.tv_name);
tv_address = (TextView) findViewById(R.id.tv_address);
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);
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);
list_poi = (ListView) findViewById(R.id.list_poi);
rl_now = (RelativeLayout) findViewById(R.id.rl_now);
rl_now.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(com.amap.api.maps.model.BitmapDescriptorFactory
.fromResource(R.drawable.point_icon));// 设置小蓝点的图标
myLocationStyle.strokeColor(getResources().getColor(R.color.transparent));
myLocationStyle.radiusFillColor(getResources().getColor(R.color.transparent));
aMap.setMyLocationStyle(myLocationStyle);
mUiSettings = aMap.getUiSettings();
//获得地图的最大和最小缩放级别
maxZoomLevel = aMap.getMaxZoomLevel();
minZoomLevel = aMap.getMinZoomLevel();
try {
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
setUpMap();
} catch (AMapException e) {
e.printStackTrace();
}
}
}
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);// 设置地图是否旋转
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
MobclickAgent.onPause(this);
}
@Override
protected void onResume() {
super.onResume();
mapView.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 onClick(View v) {
switch (v.getId()) {
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;
aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
// 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
// aMap.moveCamera();
aMap.moveCamera(CameraUpdateFactory.zoomTo(14));
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();
break;
case R.id.rl_now:
if (tag.equals("start")) {
RoutePlanShowAddressActivity.tv_start.setText(tv_address.getText().toString());
RoutePlanShowAddressActivity.start_name = tv_address.getText().toString();
RoutePlanShowAddressActivity.start_address = tv_address.getText().toString();
RoutePlanShowAddressActivity.start_jing = poi_jing;
RoutePlanShowAddressActivity.start_wei = poi_wei;
ActivityManagerApplication.destoryActivity("edit");
} else if (tag.equals("end")) {
RoutePlanShowAddressActivity.tv_end.setText(tv_address.getText().toString());
RoutePlanShowAddressActivity.end_name = tv_address.getText().toString();
RoutePlanShowAddressActivity.end_address = tv_address.getText().toString();
RoutePlanShowAddressActivity.end_jing = poi_jing;
RoutePlanShowAddressActivity.end_wei = poi_wei;
ActivityManagerApplication.destoryActivity("edit");
} else if (tag.equals("home")) {
SharedPreferences mSharedPreferences = getSharedPreferences("routeHomeInfo",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString("home_name", tv_address.getText().toString());
editor.putString("home_address", tv_address.getText().toString());
editor.putString("home_jing", poi_jing);
editor.putString("home_wei", poi_wei);
editor.commit();
ActivityManagerApplication.destoryActivity("edit");
} else if (tag.equals("company")) {
SharedPreferences mSharedPreferences = getSharedPreferences("routeCompanyInfo",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString("company_name", tv_address.getText().toString());
editor.putString("company_address", tv_address.getText().toString());
editor.putString("company_jing", poi_jing);
editor.putString("company_wei", poi_wei);
editor.commit();
ActivityManagerApplication.destoryActivity("edit");
} else {
EventBusUtil.postEvent(new PoiEvent(poi_jing, poi_wei));
ActivityManagerApplication.destoryActivity("edit");
}
finish();
break;
default:
break;
}
}
@Override
public void onCameraChange(CameraPosition cameraPosition) {
// tv_name.setText("正在获取位置,请您耐心等待...");
tv_address.setText("正在获取位置,请您耐心等待...");
rl_now.setClickable(false);
list_poi.setEnabled(false);
}
@Override
public void onCameraChangeFinish(CameraPosition cameraPosition) {
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) {
try {
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();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@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(14));
}
@Override
public void onRegeocodeSearched(RegeocodeResult result, int i) {
if (i == 1000) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
tv_address.setText(result.getRegeocodeAddress().getFormatAddress());
tv_name.setText("[当前位置]");
rl_now.setClickable(true);
pl = result.getRegeocodeAddress().getPois();
for (int a = 0; a < pl.size(); a++) {
Log.e("name===" + a, pl.get(a).getTitle());
Log.e("address===" + a, pl.get(a).getSnippet());
}
adapter = new POIAdapter();
list_poi.setAdapter(adapter);
list_poi.setEnabled(true);
}
}
}
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
}
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();
try {
tv_address.setText(amapLocation.getAddress());
tv_name.setText(amapLocation.getPoiName());
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(14));
//将地图移动到定位点
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();
}
}
}
public class POIAdapter extends BaseAdapter {
@Override
public int getCount() {
return pl.size();
}
@Override
public Object getItem(int position) {
return pl.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(getApplication()).inflate(
R.layout.item_poi, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.item_name);
viewHolder.address = (TextView) convertView.findViewById(R.id.item_address);
viewHolder.rl_poi = (RelativeLayout) convertView.findViewById(R.id.rl_poi);
viewHolder.rl_poi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tag.equals("home")) {
SharedPreferences mSharedPreferences = getSharedPreferences("routeHomeInfo",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString("home_name", pl.get(position).getTitle());
editor.putString("home_address", pl.get(position).getSnippet());
editor.putString("home_jing", pl.get(position).getLatLonPoint().getLongitude() + "");
editor.putString("home_wei", pl.get(position).getLatLonPoint().getLatitude() + "");
Log.e("enter===", pl.get(position).getLatLonPoint().getLongitude() + "" + "----" + pl.get(position).getLatLonPoint().getLatitude() + "");
editor.commit();
ActivityManagerApplication.destoryActivity("edit");
finish();
} else if (tag.equals("company")) {
SharedPreferences mSharedPreferences = getSharedPreferences("routeCompanyInfo",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString("company_name", pl.get(position).getTitle());
editor.putString("company_address", pl.get(position).getSnippet());
editor.putString("company_jing", pl.get(position).getLatLonPoint().getLongitude() + "");
editor.putString("company_wei", pl.get(position).getLatLonPoint().getLatitude() + "");
Log.e("enter===", pl.get(position).getLatLonPoint().getLongitude() + "" + "----" + pl.get(position).getLatLonPoint().getLatitude() + "");
editor.commit();
ActivityManagerApplication.destoryActivity("edit");
finish();
} else if (tag.equals("start")) {
RoutePlanShowAddressActivity.tv_start.setText(pl.get(position).getTitle());
RoutePlanShowAddressActivity.start_name = pl.get(position).getTitle();
RoutePlanShowAddressActivity.start_address = pl.get(position).getSnippet();
RoutePlanShowAddressActivity.start_jing = pl.get(position).getLatLonPoint().getLongitude() + "";
RoutePlanShowAddressActivity.start_wei = pl.get(position).getLatLonPoint().getLatitude() + "";
ActivityManagerApplication.destoryActivity("edit");
finish();
} else if (tag.equals("end")) {
RoutePlanShowAddressActivity.tv_end.setText(pl.get(position).getTitle());
RoutePlanShowAddressActivity.end_name = pl.get(position).getTitle();
RoutePlanShowAddressActivity.end_address = pl.get(position).getSnippet();
RoutePlanShowAddressActivity.end_jing = pl.get(position).getLatLonPoint().getLongitude() + "";
RoutePlanShowAddressActivity.end_wei = pl.get(position).getLatLonPoint().getLatitude() + "";
ActivityManagerApplication.destoryActivity("edit");
finish();
} else {
EventBusUtil.postEvent(new PoiEvent(String.valueOf(pl.get(position).getLatLonPoint().getLongitude()),
String.valueOf(pl.get(position).getLatLonPoint().getLatitude())));
ActivityManagerApplication.destoryActivity("edit");
finish();
}
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name.setText(pl.get(position).getTitle());
viewHolder.address.setText(pl.get(position).getSnippet());
return convertView;
}
class ViewHolder {
TextView name, address;
RelativeLayout rl_poi;
}
}
//地图缩放方法
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();
}
}
}
|