package baidumapsdk.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
/**
* 此demo用来展示如何进行地理编码搜索(用地址检索坐标)、反地理编码搜索(用坐标检索地址)
*/
public class GeoCoderDemo extends Activity implements
OnGetGeoCoderResultListener {
GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用
BaiduMap mBaiduMap = null;
MapView mMapView = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_geocoder);
CharSequence titleLable = "地理编码功能";setTitle(titleLable);
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 初始化搜索模块,注册事件监听
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(this);
}/*** 发起搜索** @param v*/public void SearchButtonProcess(View v){
if (v.getId() == R.id.reversegeocode) {
EditText lat = (EditText) findViewById(R.id.lat);
EditText lon = (EditText) findViewById(R.id.lon);
LatLng ptCenter = new LatLng((Float.valueOf(lat.getText()
.toString())), (Float.valueOf(lon.getText().toString())));
// 反Geo搜索
mSearch.reverseGeoCode(new ReverseGeoCodeOption()
.location(ptCenter));
//@param:LatLng
}else if (v.getId() == R.id.geocode){
EditText editCity = (EditText) findViewById(R.id.city);
EditText editGeoCodeKey = (EditText) findViewById(R.id.geocodekey);
// Geo搜索
mSearch.geocode(new GeoCodeOption().city(
editCity.getText().toString()).address(
editGeoCodeKey.getText().toString()));
//@param:城市+地址
}
}@Overrideprotected void onPause(){
mMapView.onPause();
super.onPause();
}@Overrideprotected void onResume(){
mMapView.onResume();
super.onResume();
}@Overrideprotected void onDestroy(){
mMapView.onDestroy();
mSearch.destroy();
super.onDestroy();
}@Overridepublic void onGetGeoCodeResult(GeoCodeResult result){
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(GeoCoderDemo.this, "抱歉,未能找到结果", Toast.LENGTH_LONG).show();
return;
}
mBaiduMap.clear();
mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.icon_marka)));
//加上覆盖物
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
.getLocation()));
//定位
String strInfo = String.format("纬度:%f 经度:%f",result.getLocation().latitude, result.getLocation().longitude);
Toast.makeText(GeoCoderDemo.this, strInfo, Toast.LENGTH_LONG).show();
//result保存地理编码的结果 城市-->坐标
}@Overridepublic void onGetReverseGeoCodeResult(ReverseGeoCodeResult result){
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(GeoCoderDemo.this, "抱歉,未能找到结果", Toast.LENGTH_LONG).show();
return;
}
mBaiduMap.clear();
mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.icon_marka)));
//加上覆盖物
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
.getLocation()));
//定位
Toast.makeText(GeoCoderDemo.this, result.getAddress(),
Toast.LENGTH_LONG).show();
//result保存翻地理编码的结果 坐标-->城市
}
}