package com.jiuqi.njt.ui;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewStub;
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.maps.AMap;
import com.amap.api.maps.AMap.OnCameraChangeListener;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.AMap.CancelableCallback;
import com.amap.api.maps.AMap.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.AMap.OnMarkerDragListener;
import com.amap.api.maps.SupportMapFragment;
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.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.search.core.LatLonPoint;
import com.jiuqi.njt.R;
import com.jiuqi.njt.util.AMapUtil;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.TitleBarUtil;
import com.jiuqi.njt.util.UIUtil;
import com.jqyd.android.module.lbs.LocationClient;
import com.jqyd.android.module.lbs.Interface.ILocationListener;
import com.jqyd.android.module.lbs.bean.LocationInfo;
/**============================================================
* 版权: 久其软件 版权所有 (c)
* 包:
* 修改记录:
* 日期 作者 内容
* =============================================================
* 2013-6-13 liyue
* ============================================================*/
/**
* <p>
* 标记地图POI界面
* </p>
*
* <p>
* Copyright: 版权所有 (c)<br>
* Company: 久其
* </p>
*
* @author liyue
* @version 2013-6-13
*/
public class PoiLocateActivity extends FragmentActivity implements
OnMarkerClickListener, OnMarkerDragListener, AMapLocationListener,
OnCameraChangeListener {
private AMap aMap;
private Marker defaultMarker;
private Marker locationdeMarker;
private LocationManagerProxy mAMapLocManager = null;//高德定位方法弃用,采用百度定位
private TextView zywzTextView;
private ImageView zywzImageView;
private double markLatitude;
private double markLongitude;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
UIUtil.showMsg(PoiLocateActivity.this, (String) msg.obj);
double latitude = msg.getData().getDouble("latitude",
Constants.ZHENGZHOU.getLatitude());
double longitude = msg.getData().getDouble("longitude",
Constants.ZHENGZHOU.getLongitude());
markLatitude = latitude;
markLongitude = longitude;
zywzTextView.setVisibility(View.VISIBLE);
zywzImageView.setVisibility(View.VISIBLE);
zywzTextView.setText("拖动地图选择位置坐标,当前位置\n");
if (null == locationdeMarker) {
locationdeMarker = aMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("您的位置")
.snippet("")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.poi_current_position))
// .icon(BitmapDescriptorFactory.fromBitmap(getViewBitmap(getView("目的地",
// "先长按标记,再拖到指定的位置"))))
.draggable(false));
locationdeMarker.showInfoWindow();
}
/*
* if (null == defaultMarker) { // 对地图添加一个marker defaultMarker =
* aMap.addMarker(new MarkerOptions() .position(new LatLng(latitude,
* longitude)) .title("作业位置").snippet("先长按标记,再拖到指定的位置")
* .icon(BitmapDescriptorFactory
* .fromResource(R.drawable.poi_current_position)) //
* .icon(BitmapDescriptorFactory
* .fromBitmap(getViewBitmap(getView("目的地", // "先长按标记,再拖到指定的位置"))))
* .draggable(true)); defaultMarker.showInfoWindow(); }
*/
updateView(new LatLng(latitude, longitude));
disableMyLocation();
}
};
private ImageView zoomin_bt;
private ImageView zoomout_bt;
private UiSettings mUiSettings;
// 切换视图
private void updateView(LatLng poi) {
CameraPosition camerapoint = new CameraPosition.Builder().target(poi)
.zoom(12).build();
changeCamera(CameraUpdateFactory.newCameraPosition(camerapoint), null);
}
/**
* 根据动画按钮状态,调用函数animateCamera或moveCamera来改变可视区
*
*/
private void changeCamera(CameraUpdate update, CancelableCallback callback) {
aMap.moveCamera(update);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doinit();
}
private void doinit() {
initParam();
initWidgets();
initListeners();
initUI();
}
private void initParam() {
}
private void initWidgets() {
setContentView(R.layout.poi_locate_activity);
ViewStub rlTitleBar = (ViewStub) findViewById(R.id.titleBarStub);
zywzTextView = (TextView) findViewById(R.id.zywzTextView);
zywzImageView = (ImageView) findViewById(R.id.zywz);
TitleBarUtil.createTitleBar(this, rlTitleBar, "标记位置", "返回",
new OnClickListener() {
@Override
public void onClick(View v) {
PoiLocateActivity.this.finish();
}
}, "确定", new OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent();
// 北京市中关村经纬度
// data.putExtra("longitude",
// defaultMarker.getPosition().longitude);
// data.putExtra("latitude",
// defaultMarker.getPosition().latitude);
setResult(RESULT_OK, data);
data.putExtra("longitude", markLongitude);
data.putExtra("latitude", markLatitude);
PoiLocateActivity.this.finish();
}
});
if (aMap == null) {
aMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (AMapUtil.checkReady(this, aMap)) {
setUpMap();
}
}
zoomin_bt = (ImageView) findViewById(R.id.zoomin_bt);
zoomout_bt = (ImageView) findViewById(R.id.zoomout_bt);
// 隐藏地图自带的缩放按钮
mUiSettings = aMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(false);
}
class CustomInfoWindowAdapter implements InfoWindowAdapter {
private final View mWindow;
private final View mContents;
CustomInfoWindowAdapter() {
mWindow = getLayoutInflater().inflate(R.layout.custom_info_window,
null);
mContents = getLayoutInflater().inflate(
R.layout.custom_info_contents, null);
}
@Override
public View getInfoWindow(Marker marker) {
render(marker, mWindow);
return mWindow;
}
@Override
public View getInfoContents(Marker marker) {
render(marker, mContents);
return mContents;
}
private void render(Marker marker, View view) {
String title = marker.getTitle();
TextView titleUi = ((TextView) view.findViewById(R.id.title));
if (title != null) {
titleUi.setText(title);
} else {
titleUi.setText("");
}
String snippet = marker.getSnippet();
TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
if (snippet != null) {
snippetUi.setText(snippet);
} else {
snippetUi.setText("");
}
}
}
private void setUpMap() {
aMap.getUiSettings().setZoomControlsEnabled(true);// 设置系统默认缩放按钮可见
// aMap.getUiSettings().setMyLocationButtonEnabled(true);
aMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
// mAMapLocManager = LocationManagerProxy.getInstance(this);
// aMap.setMyLocationEnabled(true);
}
private void initListeners() {
aMap.setOnMarkerClickListener(this);// 对marker添加点击监听器
aMap.setOnMarkerDragListener(this);
aMap.setOnCameraChangeListener(this);
zoomin_bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
aMap.animateCamera(CameraUpdateFactory.zoomIn(), 1000, null);
}
});
zoomout_bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
aMap.animateCamera(CameraUpdateFactory.zoomOut(), 1000, null);
}
});
/*
* aMap.setOnCameraChangeListener(new OnCameraChangeListener() { //
* 设置可视区域改变监听器
*
* @Override public void onCameraChange(CameraPosition cameraPosion) {
* VisibleRegion visibleRegion = aMap.getProjection()
* .getVisibleRegion(); // 获取可视区域 LatLngBounds latLngBounds =
* aMap.getProjection() .getVisibleRegion().latLngBounds;//
* 获取可视区域的Bounds boolean isContain =
* latLngBounds.contains(Constants.SHANGHAI);// 判断上海经纬度是否包括在北京区域
* Log.d(Constants.TAG, "visible region: " + visibleRegion.toString() +
* "LatLngBounds:" + latLngBounds.toString() + "上海经纬度是否在北京范围内" +
* isContain); }
*
* });
*/
}
private void initUI() {
// enableMyLocation();
}
/**
* 对marker标注点点击响应事件
*/
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.equals(defaultMarker)) {
UIUtil.showMsg(this, marker.getSnippet());
marker.showInfoWindow();
}
return false;
}
@Override
public void onMarkerDrag(Marker marker) {
// if(marker.equals(defaultMarker)){
// UIUtil.showMsg(this,
// "onMarkerDrag: "+marker.getPosition().toString());
// Log.v(Constants.TAG,
// "onMarkerDrag: "+marker.getPosition().toString());
// }
}
@Override
public void onMarkerDragStart(Marker marker) {
// if(marker.equals(defaultMarker)){
// // UIUtil.showMsg(this,
// "onMarkerDragStart: "+marker.getPosition().toString());
// Log.v(Constants.TAG,
// "onMarkerDragStart: "+marker.getPosition().toString());
// }
}
@Override
public void onMarkerDragEnd(Marker marker) {
if (marker.equals(defaultMarker)) {
// UIUtil.showMsg(this,
// "onMarkerDragEnd: "+marker.getPosition().toString());
Log.v(Constants.TAG, "onMarkerDragEnd: "
+ marker.getPosition().toString());
}
}
/**
* 把一个xml布局文件转化成view
*/
public View getView(String title, String text) {
View view = getLayoutInflater().inflate(R.layout.marker, null);
TextView text_title = (TextView) view.findViewById(R.id.marker_title);
TextView text_text = (TextView) view.findViewById(R.id.marker_text);
text_title.setText(title);
text_text.setText(text);
return view;
}
/**
* 把一个view转化成bitmap对象
*/
public static Bitmap getViewBitmap(View view) {
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
public void enableMyLocation() {
if (mAMapLocManager == null) {
mAMapLocManager = LocationManagerProxy.getInstance(this);
}
// Location API定位采用GPS和网络混合定位方式,时间最短是5000毫秒
mAMapLocManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork, 5000, 1, this);
}
public void disableMyLocation() {
if (mAMapLocManager != null) {
mAMapLocManager.removeUpdates(this);
mAMapLocManager.destory();
}
mAMapLocManager = null;
}
@Override
protected void onResume() {
super.onResume();
// enableMyLocation();
setUpBaiduLocation();
}
@Override
protected void onPause() {
super.onPause();
disableMyLocation();
}
@Override
protected void onDestroy() {
if (mAMapLocManager != null) {
mAMapLocManager.removeUpdates(this);
mAMapLocManager.destory();
}
mAMapLocManager = null;
super.onDestroy();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
/**
* 定位成功后回调函数
*/
@Override
public void onLocationChanged(AMapLocation location) {
Message msg = new Message();
if (location != null) {
Double geoLat = location.getLatitude();
Double geoLng = location.getLongitude();
String cityCode = "";
String desc = "";
String str = "";
Bundle locBundle = location.getExtras();
if (locBundle != null) {
cityCode = locBundle.getString("citycode");
desc = locBundle.getString("desc");
str = ("定位成功:(" + geoLng + "," + geoLat + ")" + "\n精 度 :"
+ location.getAccuracy() + "米" + "\n定位方式:"
+ location.getProvider() + "\n定位时间:"
+ AMapUtil.convertToTime(location.getTime())
+ "\n城市编码:" + cityCode + "\n位置描述:" + desc + "\n省:"
+ location.getProvince() + "\n市:" + location.getCity()
+ "\n区(县):" + location.getDistrict() + "\n城市编码:"
+ location.getCityCode() + "\n区域编码:" + location
.getAdCode());
msg.getData().putDouble("latitude", geoLat);
msg.getData().putDouble("longitude", geoLng);
} else {
str = "定位失败,默认位置为郑州,请自行调整";
msg.getData().putDouble("latitude",
Constants.ZHENGZHOU.getLatitude());
msg.getData().putDouble("longitude",
Constants.ZHENGZHOU.getLongitude());
}
msg.obj = str;
if (handler != null) {
handler.sendMessage(msg);
}
}
}
@Override
public void onCameraChange(CameraPosition arg0) {
// TODO Auto-generated method stub
markLatitude = arg0.target.latitude;
markLongitude = arg0.target.longitude;
zywzTextView.setText("拖动地图选择位置坐标 , 当前位置: \n\t经度:" + markLongitude + "纬度:"
+ markLatitude);
}
@Override
public void onCameraChangeFinish(CameraPosition arg0) {
// TODO Auto-generated method stub
}
private void setUpBaiduLocation() {
/**
* 接收百度定位类传来的参数
*/
LocationClient client = new LocationClient(this);
client.request(new ILocationListener() {
@Override
public void onReceiveLocInfo(int locType, LocationInfo loc) {
Message msg = new Message();
if (loc != null) {
Double geoLat = loc.getLat();
Double geoLng = loc.getLon();
String cityCode = "";
String desc = "";
String str = "";
if (loc.getSuccess() == 1) {
str = ("定位成功:(" + geoLng + "," + geoLat + ")" + "\n定位方式:"
+ loc.getPosour() + "\n定位时间:"
+ AMapUtil.convertToTime(loc.getTime())
+ "\n位置描述:" + loc.getContent() + "\n省:"
+ loc.getProvince() + "\n市:" + loc.getCity()
+ "\n区(县):" + loc.getCountry() );
msg.getData().putDouble("latitude", geoLat);
msg.getData().putDouble("longitude", geoLng);
} else {
str = "定位失败,默认位置为郑州,请自行调整";
msg.getData().putDouble("latitude",
Constants.ZHENGZHOU.getLatitude());
msg.getData().putDouble("longitude",
Constants.ZHENGZHOU.getLongitude());
}
msg.obj = str;
if (handler != null) {
handler.sendMessage(msg);
}
}
}
});
}
}