package com.ttj.activity; import android.app.Activity; import android.content.Intent; import android.location.Location; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.TextView; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationListener; import com.amap.api.location.LocationManagerProxy; import com.amap.api.location.LocationProviderProxy; import com.amap.api.maps2d.AMap; import com.amap.api.maps2d.AMap.OnMapClickListener; import com.amap.api.maps2d.AMap.OnMarkerDragListener; import com.amap.api.maps2d.LocationSource; import com.amap.api.maps2d.MapView; import com.amap.api.maps2d.model.BitmapDescriptorFactory; import com.amap.api.maps2d.model.LatLng; import com.amap.api.maps2d.model.Marker; import com.amap.api.maps2d.model.MarkerOptions; import com.ttj.R; import com.ttj.constants.ResultCode; import com.ttj.utils.LogUtils; import com.ttj.utils.ToastUtil; /** * 选取位置 */ public class User_RegPosiAty extends Activity implements OnMarkerDragListener, LocationSource, AMapLocationListener, OnMapClickListener { private AMap aMap; private MapView mapView; private OnLocationChangedListener mListener; private LocationManagerProxy mAMapLocationManager; private ImageView imgBack; private TextView textOK; private double latitude = 0, longitude = 0; private AMapLocation mAMapLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.p2_main_regist_location_layout); imgBack = (ImageView) findViewById(R.id.p2_img_regist_location_title_back); textOK = (TextView) findViewById(R.id.p2_text_regist_location_title_ok); mapView = (MapView) findViewById(R.id.map); mapView.onCreate(savedInstanceState); // 此方法必须重写 init(); aMap.setLocationSource(this);// 设置定位监听 setUpMap(); ToastUtil.show(this, "请在地图上选择位置", 500); imgBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { User_RegPosiAty.this.finish(); } }); textOK.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (latitude != 0 || longitude != 0) { Intent intent = new Intent(); intent.putExtra("latitude", latitude); intent.putExtra("longitude", longitude); setResult(ResultCode.Map_ReqCode, intent); User_RegPosiAty.this.finish(); // User_RegistAty.sh_longitude=longitude; // User_RegistAty.sh_latitude=latitude; // User_RegPosiAty.this.finish(); } else { ToastUtil.show(User_RegPosiAty.this, "请在地图上选择位置", 500); } } }); } private void setUpMap() { aMap.setOnMarkerDragListener(this); aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示 aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false aMap.setOnMapClickListener(this); } /** * 初始化AMap对象 */ private void init() { if (aMap == null) { aMap = mapView.getMap(); } } /** * 方法必须重写 */ @Override protected void onResume() { super.onResume(); mapView.onResume(); } /** * 方法必须重写 */ @Override protected void onPause() { super.onPause(); mapView.onPause(); deactivate(); } /** * 方法必须重写 */ @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } /** * 方法必须重写 */ @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); } /** * 在地图上添加marker * @param mAMapLocation2 */ private void addMarkersToMap(LatLng arg0, AMapLocation mAMapLocation2) { aMap.clear(); aMap.addMarker(new MarkerOptions() .anchor(0.5f, 0.5f) .title("选择地址:"+mAMapLocation2.getAddress()) .position(arg0) .draggable(true) .period(10) .icon(BitmapDescriptorFactory .defaultMarker(BitmapDescriptorFactory.HUE_RED))); aMap.setOnMarkerDragListener(this);// 设置marker可拖拽事件监听器 } /** * 监听拖动marker时事件回调 */ @Override public void onMarkerDrag(Marker marker) { } /** * 监听拖动marker结束事件回调 */ @Override public void onMarkerDragEnd(Marker marker) { latitude = marker.getPosition().latitude; longitude = marker.getPosition().longitude; } /** * 监听开始拖动marker事件回调 */ @Override public void onMarkerDragStart(Marker marker) { } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } /** * 定位成功后回调函数 */ @Override public void onLocationChanged(AMapLocation aLocation) { if (mListener != null && aLocation != null) { mListener.onLocationChanged(aLocation);// 显示系统小蓝点 mAMapLocation=aLocation; } } /** * 激活定位 */ @SuppressWarnings("deprecation") @Override public void activate(OnLocationChangedListener listener) { mListener = listener; if (mAMapLocationManager == null) { mAMapLocationManager = LocationManagerProxy.getInstance(this); /* * mAMapLocManager.setGpsEnable(false); * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location * API定位采用GPS和网络混合定位方式 * ,第一个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者 */ mAMapLocationManager.requestLocationUpdates( LocationProviderProxy.AMapNetwork, -1, 100, this); } } /** * 停止定位 */ @SuppressWarnings("deprecation") @Override public void deactivate() { mListener = null; if (mAMapLocationManager != null) { mAMapLocationManager.removeUpdates(this); mAMapLocationManager.destory(); } mAMapLocationManager = null; } @Override public void onMapClick(LatLng posit) { latitude = posit.latitude; longitude = posit.longitude; if(mAMapLocation!=null){ addMarkersToMap(posit,mAMapLocation); } } }