用Android自带的LocationManager和Location获取位置的时候,经常会有获取的location为null的情况,并且操作起来也不是很方便,在这个Demo里我使用了百度地图API中的定位SDK,可以一次性获取当前位置经纬度以及详细地址信息,还可以获取周边POI信息,同时可以设定位置通知点,当到达某一位置时,发出通知信息等方式来告知用户。。麻烦大家,下载一个baidumapapi.jar,代码如下,验证通过非常理想,学习使人进步,谢谢热心帮助的人。效果图如下:
package BD.map;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
public class BaiduMap extends Activity {
public TextView tv1, tv2;
public BMapManager mapManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1=(TextView)findViewById(R.id.result1);
tv2 = (TextView) this.findViewById(R.id.result2);
mapManager=new BMapManager(this);
mapManager.init(“53351EE4BDE7BD870F41A0B4AF1480F1CA97DAF9”, new MyMKGeneralListener());
mapManager.getLocationManager().setNotifyInternal(20, 5);
mapManager.getLocationManager().requestLocationUpdates(new MyLocationListener());
mapManager.start();
// mapManager = new BMapManager(this);
// mapManager.init(“EDB67AD764D300895C95ABA02A4DDC58D5485CCD”,
// new MyMKGeneralListener());
// // 设置通知间隔:iMaxSecond – 最大通知间隔,单位:秒;iMinSecond – 最小通知间隔,单位:秒
// mapManager.getLocationManager().setNotifyInternal(20, 5);
//
// mapManager.getLocationManager().requestLocationUpdates(
// new MyLocationListener());
// mapManager.start();
}
// 定位自己的位置,只定位一次
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location arg0) {
double jindu1 = arg0.getLatitude();
double weidu1 = arg0.getLongitude();
int jindu = (int) (arg0.getLatitude()*1000000);
int weidu = (int) (arg0.getLongitude()*1000000);
tv1.setText(“经度:” + jindu1 + “,纬度:” + weidu1);
System.out.println(“经度:” + jindu1 + “,纬度:” + weidu1);
MKSearch search = new MKSearch();
search.init(mapManager, new MyMKSearchListener());
search.reverseGeocode(new GeoPoint(jindu, weidu));
}
}
class MyMKSearchListener implements MKSearchListener {
@Override
public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
if (arg0 == null) {
tv2.setText(“没有获取想要的位置”);
} else {
GeoPoint point = arg0.geoPt;
tv2.setText(“地址:” + arg0.strAddr + “,坐标:”
+ point.getLatitudeE6() + “,” + point.getLongitudeE6());
}
}
@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 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 onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// TODO Auto-generated method stub
}
}
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
class MyMKGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int arg0) {
}
@Override
public void onGetPermissionState(int arg0) {
}
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”BD.map”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.BaiduMap”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
<!– 访问网络的权限 –>
<uses-permission android:name=”android.permission.INTERNET” />
<!– 访问精确位置的权限 –>
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<!– 访问网络状态的权限 –>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
<!– 访问WIFI网络状态的权限 –>
<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />
<!– 改变WIFI网络状态的权限 –>
<uses-permission android:name=”android.permission.CHANGE_WIFI_STATE” />
<!– 读写存储卡的权限 –>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
<!– 读取电话状态的权限 –>
<uses-permission android:name=”android.permission.READ_PHONE_STATE” />
<!– 添加屏幕支持 –>>
<supports-screens android:largeScreens=”true”
android:normalScreens=”true” android:smallScreens=”true”
android:resizeable=”true” android:anyDensity=”true”/>
</manifest>