问题描述
- 新手求助,android百度定位textview没有显示地址
-
public class StartCanteenActivity extends Activity
{
private TextView locationInfoTextView = null;
private Button startButton = null;
private LocationClient locationClient = null;
private static final int UPDATE_TIME = 5000;
private static int LOCATION_COUTNS = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_canteen);
locationInfoTextView = (TextView) this.findViewById(R.id.locationTextView);
startButton = (Button) this.findViewById(R.id.location_button);locationClient = new LocationClient(this); //设置定位条件 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); //是否打开GPS option.setCoorType("bd09ll"); //设置返回值的坐标类型 option.setPriority(LocationClientOption.NetWorkFirst); //设置定位优先级 option.setScanSpan(UPDATE_TIME); //设置定时定位的时间间隔。单位毫秒 locationClient.setLocOption(option); //注册位置监听器 locationClient.registerLocationListener(new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { if (location == null) { return; } StringBuffer sb = new StringBuffer(256); sb.append(" Address : "); sb.append(location.getAddrStr()); sb.append(String.valueOf(LOCATION_COUTNS)); locationInfoTextView.setText(sb.toString()); } }); startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (locationClient == null) { return; } if (locationClient.isStarted()) { startButton.setText("Start"); locationClient.stop(); } else { startButton.setText("Stop"); locationClient.start(); locationClient.requestLocation(); } } }); } @Override protected void onDestroy() { super.onDestroy(); if (locationClient != null && locationClient.isStarted()) { locationClient.stop(); locationClient = null; } }
}
解决方案
解决方案二:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
public class MainActivity extends Activity {
private BaiduMap baiduMap;
private MapView mapView;
private LocationClient locationClient = null;// 定位SDK的核心类
private MyLocationData myLocationData;// 用户位置信息
private String tempcoor = "gcj02";
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());// 初始化SDK
setContentView(R.layout.activity_main);
init();
}
public void init() {
mapView = (MapView) findViewById(R.id.mapView);
textView = (TextView) findViewById(R.id.textView1);
baiduMap = mapView.getMap();// 得到百度地图
// 设置百度地图类型
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);// 普通地图
// baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);// 卫星地图
// baiduMap.setTrafficEnabled(true);// 开启交通图
// baiduMap.setBaiduHeatMapEnabled(true);//开启地图热度,人口密集处
// baiduMap.setMyLocationEnabled(true);// 开启定位图层
locationClient = new LocationClient(getApplicationContext());
locationClient.registerLocationListener(bdLocationListener);// 注册定位监听器
// 定位前的准备工作
initLocation();
locationClient.start();// 开始定位
locationClient.requestLocation();
}
public void initLocation() {
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);// 高精度定位模式
option.setCoorType(tempcoor);// 国测局加密经纬度坐标
option.setScanSpan(0);
option.setIsNeedAddress(true);
option.setOpenGps(true);
option.setLocationNotify(true);// 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIgnoreKillProcess(true);
locationClient.setLocOption(option);
}
BDLocationListener bdLocationListener = new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (location == null) {
return;
}
// myLocationData = new MyLocationData.Builder()
// .accuracy(location.getRadius()).direction(100)
// .latitude(location.getLatitude())
// .latitude(location.getLongitude()).build();
// baiduMap.setMyLocationData(myLocationData);
textView.setText("radius:" + location.getRadius() + ";"
+ "latitute:" + location.getLatitude() + ";"
+ "longtitute:" + location.getLongitude() + ";"
+ "address:" + location.getAddrStr());
}
};
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (mapView != null) {
mapView.onResume();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mapView != null) {
mapView.onPause();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (mapView != null) {
mapView.onDestroy();
mapView = null;
}
if (locationClient != null) {
locationClient.unRegisterLocationListener(bdLocationListener);
}
}
}
解决方案三:
参考:
百度地图开发:
http://blog.csdn.net/column/details/android-jacksen-map.html
时间: 2025-01-27 00:21:46