原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/Android/2019/0525/508.html
本以为很繁难的,结果即是百度曾经供应了:地舆编码&反地舆编码的接口。
mSearch.geocode(new GeoCodeOption().city(
editCity.getText().toString()).address(
editGeoCodeKey.getText().toString()));
mSearch.reverseGeoCode(new ReverseGeoCodeOption()
.location(ptCenter));
之后只必要重写onGetGeoCodeResult(GeoCodeResult result)和onGetReverseGeoCodeResult(ReverseGeoCodeResult result)就可以对正反编码举行自定义处分
完整代码:
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:城市+地点
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
mMapView.onDestroy();
mSearch.destroy();
super.onDestroy();
}
@Override
public 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留存地舆编码的结果 城市-->坐标
}
@Override
public 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留存翻地舆编码的结果 坐标-->城市
}
}
上篇:上一篇:Android Accessibility辅助功效类的学习
下篇:下一篇:checkBox的状况掌握按钮能否点击置灰