百度地图开放平台地址:http://developer.baidu.com/map/
下载Android定位SDK:
java代码:
package com.example.baidumap; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import android.app.Activity; import android.location.Location; import android.location.LocationListener; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends Activity { private LocationClient mLocationClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocationClient = new LocationClient(this); LocationClientOption option = new LocationClientOption(); //可选,设置是否需要地址信息,默认不需要 option.setIsNeedAddress(true); option.setAddrType("all"); mLocationClient.setLocOption(option); mLocationClient.registerLocationListener(new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { //当前设备位置所在的省 String province = location.getProvince(); //当前设备位置所在的市 String city = location.getCity(); //当前设备位置所在街道 String addr = location.getAddrStr(); Toast.makeText(MainActivity.this, province+","+city+","+addr, Toast.LENGTH_SHORT).show(); Log.d("当前地址:", province+","+city+","+addr); } }); mLocationClient.start(); } @Override protected void onDestroy() { super.onDestroy(); mLocationClient.stop(); } }
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.baidumap" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <!-- 这个权限用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > </uses-permission> <!-- 这个权限用于访问GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission> <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission> <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 --> <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" /> <!-- SD卡读取权限,用户写入离线定位数据 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" > </uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="QY92flFYlkP8v2tUEwLhWftZ" /> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > </service> </application> </manifest>