百度地图获取周边信息
--------------------编程问答-------------------- 第二个界面 获取信息
这是第一个界面,摇一下就入获取周边信息,这个没有问题,就是进入第二个界面就停止运行,第二个界面单独是可以的
package com.example.xunyao;
·····
public class MainActivity extends Activity implements SensorEventListener{
private LocationClient mLocationClient = null;
private TextView mTextView = null;
SensorManager sensorManager = null;
Vibrator vibrator = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// mStartBtn = (Button)findViewById(R.id.button1);
mTextView = (TextView)findViewById(R.id.textView);
mLocationClient = new LocationClient(this);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); //打开gps
option.setCoorType("bd09ll"); //设置坐标类型为bd09ll
option.setPriority(LocationClientOption.NetWorkFirst); //设置网络优先
option.setProdName("locSDKDemo2"); //设置产品线名称
option.setScanSpan(5000); //定时定位,每隔5秒钟定位一次。
mLocationClient.setLocOption(option);
mLocationClient.start();
mLocationClient.registerLocationListener(new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return ;
StringBuffer sb = new StringBuffer(256);
if (location.getLocType() == BDLocation.TypeGpsLocation){
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append(location.getAddrStr());
}
mTextView.setText(sb.toString());
}
public void onReceivePoi(BDLocation location){
return ;
}
});
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
}
@Override
public void onDestroy(){
if (mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.stop();
mLocationClient = null;
}
super.onDestroy();
}
@Override
protected void onPause()
{
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
protected void onResume()
{
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
//当传感器精度改变时回调该方法,Do nothing.
}
@Override
public void onSensorChanged(SensorEvent event)
{
int sensorType = event.sensor.getType();
//values[0]:X轴,values[1]:Y轴,values[2]:Z轴
float[] values = event.values;
if (sensorType == Sensor.TYPE_ACCELEROMETER)
{
if ((Math.abs(values[0]) > 17 || Math.abs(values[1]) > 17 || Math
.abs(values[2]) > 17))
{
//摇动手机后,再伴随震动提示~~
vibrator.vibrate(500);
Intent i=new Intent();
i.setClass(MainActivity.this, Yao.class);
startActivity(i);
}
}}
}
package com.example.xunyao;
····
public class Yao extends Activity {
private static final String TAG = "MainActivity";
public static MApplication mApp;
private static final String key = "3E72E3DEBBFC01B7222FF488F1FB7B7831380D4B";
private BMapManager mapManager;
private MKLocationManager mLocationManager;
private GeoPoint mGPoint;
private boolean IS_LOCATIONED = false;
private MKSearch mSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yao);
this.initMap();
}
// 初始化地图相关
public void initMap() {
mapManager = new BMapManager(this);
boolean isSuccess = mapManager.init(key, null);
if (isSuccess) {
mapManager.getLocationManager().setNotifyInternal(10, 5);
}
//bMapView = (MapView) findViewById(R.id.bmapView);
//mapManager = mApp.getMapManager();
if (mapManager == null) {
//mApp.initMapManager();
}
mapManager.start();
// 定位
mLocationManager = mapManager.getLocationManager();
mLocationManager.requestLocationUpdates(mLocationListener);
// 设置搜索
mSearch = new MKSearch();
mSearch.init(mapManager, mSearchListener);
}
// 监听定位
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
mGPoint = new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
mSearch.reverseGeocode(new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6)));
// 设置中心点一次
if (!IS_LOCATIONED) {
//bMapView.getController().setCenter(mGPoint);
}
IS_LOCATIONED = true;
}
}
};
// 监听搜索
private MKSearchListener mSearchListener = new MKSearchListener() {
/**
* res : 结果集 type 结果类型: MKSearch.TYPE_POI_LIST城市内搜索poi列表
* MKSearch.TYPE_AREA_POI_LIST 范围搜索、周边搜索poi列表
* MKSearch.TYPE_AREA_MULTI_POI_LIST 多关键字范围搜索、周边搜索poi列表 iError :非0都是错误的
*/
@Override
public void onGetAddrResult(MKAddrInfo result, int iError) {
// TODO Auto-generated method stub
if (result == null) {
return;
}
StringBuffer sb = new StringBuffer();
// 经纬度所对应的位置
sb.append(result.strAddr).append("/n");
//所在位置
TextView nTextView1 = (TextView)findViewById(R.id.textV11);
//////////////////////////////////////////
if (null != result.poiList) {
// 遍历所有的兴趣点信息
for (MKPoiInfo poiInfo : result.poiList) {
sb.append("名称:").append(poiInfo.name).append("/n");
sb.append("地址:").append(poiInfo.address).append("/n");
sb.append("经度:").append(poiInfo.pt.getLongitudeE6() / 1000000.0f).append("/n");
sb.append("纬度:").append(poiInfo.pt.getLatitudeE6() / 1000000.0f).append("/n");
sb.append("电话:").append(poiInfo.phoneNum).append("/n");
sb.append("邮编:").append(poiInfo.postCode).append("/n");
// poi类型,0:普通点,1:公交站,2:公交线路,3:地铁站,4:地铁线路
sb.append("类型:").append(poiInfo.ePoiType).append("/n");
nTextView1.setText(poiInfo.name);
}
}
////////////////////////////////////////////////////////////
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub //
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
if (mapManager != null) {
mapManager.stop();
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
mapManager = mApp.getMapManager();
mapManager.start();
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
--------------------编程问答--------------------
第二个界面 获取信息
package com.example.xunyao;
····
public class Yao extends Activity {
private static final String TAG = "MainActivity";
public static MApplication mApp;
private static final String key = "3E72E3DEBBFC01B7222FF488F1FB7B7831380D4B";
private BMapManager mapManager;
private MKLocationManager mLocationManager;
private GeoPoint mGPoint;
private boolean IS_LOCATIONED = false;
private MKSearch mSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yao);
this.initMap();
}
// 初始化地图相关
public void initMap() {
mapManager = new BMapManager(this);
boolean isSuccess = mapManager.init(key, null);
if (isSuccess) {
mapManager.getLocationManager().setNotifyInternal(10, 5);
}
//bMapView = (MapView) findViewById(R.id.bmapView);
//mapManager = mApp.getMapManager();
if (mapManager == null) {
//mApp.initMapManager();
}
mapManager.start();
// 定位
mLocationManager = mapManager.getLocationManager();
mLocationManager.requestLocationUpdates(mLocationListener);
// 设置搜索
mSearch = new MKSearch();
mSearch.init(mapManager, mSearchListener);
}
// 监听定位
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
mGPoint = new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
mSearch.reverseGeocode(new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6)));
// 设置中心点一次
if (!IS_LOCATIONED) {
//bMapView.getController().setCenter(mGPoint);
}
IS_LOCATIONED = true;
}
}
};
// 监听搜索
private MKSearchListener mSearchListener = new MKSearchListener() {
/**
* res : 结果集 type 结果类型: MKSearch.TYPE_POI_LIST城市内搜索poi列表
* MKSearch.TYPE_AREA_POI_LIST 范围搜索、周边搜索poi列表
* MKSearch.TYPE_AREA_MULTI_POI_LIST 多关键字范围搜索、周边搜索poi列表 iError :非0都是错误的
*/
@Override
public void onGetAddrResult(MKAddrInfo result, int iError) {
// TODO Auto-generated method stub
if (result == null) {
return;
}
StringBuffer sb = new StringBuffer();
// 经纬度所对应的位置
sb.append(result.strAddr).append("/n");
//所在位置
TextView nTextView1 = (TextView)findViewById(R.id.textV11);
//////////////////////////////////////////
if (null != result.poiList) {
// 遍历所有的兴趣点信息
for (MKPoiInfo poiInfo : result.poiList) {
sb.append("名称:").append(poiInfo.name).append("/n");
sb.append("地址:").append(poiInfo.address).append("/n");
sb.append("经度:").append(poiInfo.pt.getLongitudeE6() / 1000000.0f).append("/n");
sb.append("纬度:").append(poiInfo.pt.getLatitudeE6() / 1000000.0f).append("/n");
sb.append("电话:").append(poiInfo.phoneNum).append("/n");
sb.append("邮编:").append(poiInfo.postCode).append("/n");
// poi类型,0:普通点,1:公交站,2:公交线路,3:地铁站,4:地铁线路
sb.append("类型:").append(poiInfo.ePoiType).append("/n");
nTextView1.setText(poiInfo.name);
}
}
////////////////////////////////////////////////////////////
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub //
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
// TODO Auto-generated method stub
}
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
if (mapManager != null) {
mapManager.stop();
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
mapManager = mApp.getMapManager();
mapManager.start();
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
补充:Java , Eclipse