以下我说说在百度地图上实现定位。
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
public class MainActivity extends Activity {
private TextView mTv = null;
public LocationClient mLocationClient = null;
public MyLocationListenner myListener = new MyLocationListenner();
public Button ReLBSButton=null;
public static String TAG = "msg";
BMapManager mBMapMan = null;
MapView mMapView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//地图
mBMapMan=new BMapManager(getApplication());
mBMapMan.init(null);
setContentView(R.layout.activity_main);
//地图
mMapView=(MapView)findViewById(R.id.bmapsView);
mMapView.setBuiltInZoomControls(true); //实现放大缩小地图的功能
mMapView.setTraffic(true); //显示交通状况
//mMapView.setSatellite(true); //显示卫星地图
MapController mMapController=mMapView.getController();
// 得到mMapView的控制权,能够用它控制和驱动平移和缩放
GeoPoint point =new GeoPoint((int)(39.915* 1E6),(int)(116.404* 1E6));
//用给定的经纬度构造一个GeoPoint。单位是微度 (度 * 1E6)
mMapController.setCenter(point);//设置地图中心点
mMapController.setZoom(12);//设置地图zoom级别
mTv = (TextView)findViewById(R.id.textview);
ReLBSButton=(Button)findViewById(R.id.ReLBS_button);
mLocationClient = new LocationClient( getApplicationContext() );
/**——————————————————————————————————————————————————————————————————
* 这里的AK和应用签名包名绑定,假设使用在自己的project中须要替换为自己申请的Key
* ——————————————————————————————————————————————————————————————————
*/
// mLocationClient.setAK("697f50541f8d4779124896681cb6584d");
// mLocationClient.setAK("z4nqERrqxnhNzT5VOGNVRt80");
mLocationClient.registerLocationListener( myListener );
setLocationOption();//设定定位參数
mLocationClient.start();//開始定位
System.out.println(1);
// 又一次定位
ReLBSButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.requestLocation();
System.out.println(3); }
else
Log.d("msg", "locClient is null or not started");
}
});
}
//设置相关參数
private void setLocationOption(){
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setAddrType("all");//返回的定位结果包括地址信息
option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(50000);//设置发起定位请求的间隔时间为5000ms
option.disableCache(true);//禁止启用缓存定位
option.setPoiNumber(5); //最多返回POI个数
option.setPoiDistance(1000); //poi查询距离
option.setPoiExtraInfo(true); //是否须要POI的电话和地址等具体信息
mLocationClient.setLocOption(option);
}
@Override
public void onDestroy() {
mLocationClient.stop();//停止定位
mMapView.destroy();
if(mBMapMan!=null){
mBMapMan.destroy();
mBMapMan=null;
}
mTv = null;
super.onDestroy();
}
/**
* 监听函数,有更新位置的时候。格式化成字符串。输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
@Override
//接收位置信息
public void onReceiveLocation(BDLocation location) {
if (location == null)
return ;
StringBuffer sb = new StringBuffer(256);
sb.append("1时间 : ");
sb.append(location.getTime());
sb.append("
1return code : ");
sb.append(location.getLocType());
sb.append("
1latitude : ");
sb.append(location.getLatitude());
sb.append("
1lontitude : ");
sb.append(location.getLongitude());
sb.append("
1radius : ");
sb.append(location.getRadius());
System.out.println(2);
if (location.getLocType() == BDLocation.TypeGpsLocation){
sb.append("
2speed : ");
sb.append(location.getSpeed());
sb.append("
2satellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
/**
* 格式化显示地址信息
*/
sb.append("
3addr : ");
sb.append(location.getAddrStr());
}
sb.append("
4sdk version : ");
sb.append(mLocationClient.getVersion());
sb.append("
4isCellChangeFlag : ");
sb.append(location.isCellChangeFlag());
mTv.setText(sb.toString());
Log.i(TAG, sb.toString());
mMapView.getController().setCenter(new GeoPoint((int)(29.52* 1E6), (int)(106.57* 1E6)));
//mMapView.getController().setZoom(12);
mMapView.refresh();
}
//接收POI信息函数,我不须要POI,所以我没有做处理
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null) {
return;
}
}
}
}
在这段代码里我没有进行实时定位。仅仅是给地图传了一个经纬度在地图上显示而已,东西弄的比較水。看看效果吧
上面是定位的经纬度想,以下是地图。
记住百度定位sdk4.0以上要申请密钥才干使用,还有。密钥不是在程序里给定而是在manifest文件里给定 <meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="HP3YeKaxrdwZzZCDHC5sR6lo" />
以下给出manifest文件吧
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bdgps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="HP3YeKaxrdwZzZCDHC5sR6lo" />
<activity
android:name="com.example.bdgps.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
</application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" >
</uses-permission>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
</manifest>
在.xml文件里记得定义好mapview,以下给出.xml文件<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textview" android:layout_width="194dp" android:layout_height="wrap_content" /> <Button android:id="@+id/ReLBS_button" android:layout_width="194dp" android:layout_height="wrap_content" android:text="Update My Location" /> <com.baidu.mapapi.map.MapView android:id="@+id/bmapsView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </LinearLayout>
源代码下载:http://download.csdn.net/detail/u011833422/7221291