高德地图,点地图上任意一点,出现marker图标,并显示出地图的文字地址。同时能得到经纬度。能够搜索地址。
/**
* Created by 张玉水 on 2016/7/18.
*/
public class ServiceLocationActivity extends TitleBaseActicity implements AMap.OnMapClickListener, AMap.OnCameraChangeListener, LocationSource, AMapLocationListener, View.OnClickListener, GeocodeSearch.OnGeocodeSearchListener {
private MyEditText service_location_myet;
public static MapView mapView;
private AMap aMap;
private ScrollView scrollView;
private MapContainer map_container;
private Marker locationMarker;
private Handler handler = new Handler();
private AMapLocationClient mLocationClient;
private AMapLocationClientOption mLocationOption;
private boolean isFirstLoc=true;
private OnLocationChangedListener mListener;
private TextView tv_conform;
private TextView tv_cancel;
private GeocodeSearch geocoderSearch;
private MarkerOptions marker;
private LatLng markerLatLng;
private String addressName;
private TextView tv_undermap;
private ImageView iv_search;
private EditText et_address_search;
@Override
public void onCreateActionBar(Context context) {
setCommonActionBar("服务地址");
}
@Override
public void onActionBarCreated(View view) {
super.onActionBarCreated(view);
view.findViewById(R.id.actionbar_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ComplteUserActivity.actionComplteUser(ServiceLocationActivity.this);
finish();
}
});
}
public static void actionServiceLocation(Activity context){
Intent intent = new Intent(context , ServiceLocationActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service_location);
scrollView = (ScrollView) findViewById(R.id.scrollview);
map_container = (MapContainer) findViewById(R.id.map_container);
tv_conform = (TextView) findViewById(R.id.confirm);
tv_cancel = (TextView) findViewById(R.id.cancel);
tv_undermap = (TextView) findViewById(R.id.tv_undermap);
iv_search = (ImageView) findViewById(R.id.iv_search);
et_address_search = (EditText) findViewById(R.id.et_address_search);
tv_conform.setOnClickListener(this);
tv_cancel.setOnClickListener(this);
iv_search.setOnClickListener(this);
map_container.setScrollView(scrollView);
service_location_myet = (MyEditText) findViewById(R.id.service_location_myet);
service_location_myet.hint("我们将感觉该地址为您优先推荐附近的客源");
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
init();
}
/**
* 初始化AMap对象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
setUpMap();
}
double[] location = new GetLocationUtils(this).getLocation();
LatLng latlng;
if (location!=null){
latlng= new LatLng(location[1],location[0]);
}else {
latlng=new LatLng(39.90403, 116.407525);
}
aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(
latlng,18,0,30)));
}
/**
* **往地图上添加一个marker**
*/
private void setUpMapMarker(LatLng latLng) {
markerLatLng=latLng;
LatLonPoint latlonpoint=new LatLonPoint(latLng.latitude,latLng.longitude);
RegeocodeQuery query = new RegeocodeQuery(latlonpoint, 200f,GeocodeSearch.AMAP);
geocoderSearch.getFromLocationAsyn(query);
aMap.clear();
marker = new MarkerOptions().position(markerLatLng)
.title("您选择的位置").snippet("正在查找...")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
aMap.addMarker(marker).showInfoWindow();
}
/**
* 往地图上添加多个marker.并带有描述
*/
private void addMapMarkersBySearch(LatLng latLng[],String desc[]) {
aMap.clear();
for (int i=0;i<latLng.length;i++){
MarkerOptions marker = new MarkerOptions().position(latLng[i])
.title("搜索的位置").snippet(desc[i])
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
aMap.addMarker(marker).showInfoWindow();
}
}
/**
* amap添加一些事件监听器
*/
private void setUpMap() {
aMap.setOnMapClickListener(this);
aMap.setOnCameraChangeListener(this);
UiSettings settings = aMap.getUiSettings();
aMap.setLocationSource(this);
settings.setMyLocationButtonEnabled(true);
aMap.setMyLocationEnabled(true);
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
initLocation();
}
public void initLocation(){
showProgressDialog(this);
mLocationClient = new AMapLocationClient(getApplicationContext());
mLocationClient.setLocationListener(this);
mLocationOption = new AMapLocationClientOption();
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
mLocationOption.setGpsFirst(false);
mLocationOption.setNeedAddress(true);
mLocationOption.setOnceLocation(true);
mLocationOption.setWifiActiveScan(true);
mLocationOption.setMockEnable(false);
mLocationOption.setInterval(2000);
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();
}
/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必须重写
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
mapView=null;
mLocationClient.stopLocation();
mLocationClient.onDestroy();
}
/**
* 对单击地图事件回调
*/
@Override
public void onMapClick(LatLng latLng) {
setUpMapMarker(latLng);
}
/**
* 对正在移动地图事件回调
*/
@Override
public void onCameraChange(CameraPosition cameraPosition) {
}
/**
* 对移动地图结束事件回调
*/
@Override
public void onCameraChangeFinish(CameraPosition cameraPosition) {
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
mListener = onLocationChangedListener;
}
@Override
public void deactivate() {
mListener=null;
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
dismissProgressDialog();
addressName = "";
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
aMapLocation.getLocationType();
aMapLocation.getLatitude();
aMapLocation.getLongitude();
aMapLocation.getAccuracy();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(aMapLocation.getTime());
df.format(date);
aMapLocation.getAddress();
aMapLocation.getCountry();
aMapLocation.getProvince();
aMapLocation.getCity();
aMapLocation.getDistrict();
aMapLocation.getStreet();
aMapLocation.getStreetNum();
aMapLocation.getCityCode();
aMapLocation.getAdCode();
if (isFirstLoc) {
aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
mListener.onLocationChanged(aMapLocation);
StringBuffer buffer = new StringBuffer();
buffer.append(
aMapLocation.getCity() + ""
+ aMapLocation.getDistrict() + ""
+ aMapLocation.getStreet() + ""
+ aMapLocation.getStreetNum());
addressName=buffer.toString();
isFirstLoc = false;
}
} else {
Toast.makeText(getApplicationContext(), "定位失败", Toast.LENGTH_LONG).show();
}
showInUnderTextView(addressName);
}
}
private void showInUnderTextView(String addressName) {
tv_undermap.setText("已为您定位到“"+addressName+"”,我们将默认您的服务地址在该地点附近," +
"并优先推荐该地点附近的客户给您。同意请点击“确认”,如需修改,请重新输入定位。");
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.confirm:
break;
case R.id.cancel:
break;
case R.id.iv_search:
GeocodeSearched();
break;
}
}
private void GeocodeSearched() {
String addressSearch = et_address_search.getText().toString().trim();
GeocodeQuery query = new GeocodeQuery(addressSearch, "010");
geocoderSearch.getFromLocationNameAsyn(query);
}
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
addressName = "";
if (rCode == 1000) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
addressName = result.getRegeocodeAddress().getFormatAddress()
+ "附近";
} else {
addressName ="获取地址错误";
ToastUtil.showNormalShortToast(addressName);
}
} else {
addressName ="获取地址错误";
ToastUtil.showNormalShortToast(addressName);
}
showInUnderTextView(addressName);
aMap.clear();
marker = new MarkerOptions().position(markerLatLng)
.title("您选择的位置").snippet(addressName)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
aMap.addMarker(marker).showInfoWindow();
}
@Override
public void onGeocodeSearched(GeocodeResult result, int rCode) {
if (rCode == 1000) {
if (result != null && result.getGeocodeAddressList() != null
&& result.getGeocodeAddressList().size() > 0) {
List<GeocodeAddress> geocodeAddressList = result.getGeocodeAddressList();
LatLng[] latLngs=new LatLng[geocodeAddressList.size()];
String[] descs=new String[geocodeAddressList.size()];
for (int i=0;i<geocodeAddressList.size();i++) {
GeocodeAddress address = geocodeAddressList.get(i);
LatLonPoint latLonPoint = address.getLatLonPoint();
LatLng latLng = new LatLng(latLonPoint.getLatitude(), latLonPoint.getLongitude());
String addressDesc = address.getFormatAddress();
latLngs[i]=latLng;
descs[i]=addressDesc;
}
aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(
latLngs[0], 18, 0, 30)));
addMapMarkersBySearch(latLngs,descs);
} else {
ToastUtil.showNormalShortToast("网络不好,搜索失败!");
}
} else {
ToastUtil.showNormalShortToast("网络不好,搜索失败!");
}
}
}