Android获取当前位置,推荐使用百度地图SDK获取位置

这里直接引用了百度V2.6出给出的DEMO实例,经过测试,在手机客户端可以获取到当前所在的位置 首先先看一下MainActivity.java
package com.example.locationmanager;

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.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private LocationClient mLocationClient = null;
private Button mStartBtn = null;
private TextView mTextView = 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.textview1);

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.registerLocationListener(new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return ;
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
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("\naddr : ");
sb.append(location.getAddrStr());
}
sb.append("\nsdk version : ");
sb.append(mLocationClient.getVersion());
mTextView.setText(sb.toString());
}

public void onReceivePoi(BDLocation location){
//return ;
}
});


mStartBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mLocationClient == null) return ;
if (mLocationClient.isStarted()){
mLocationClient.stop();
mStartBtn.setText("开始");
} else {
mLocationClient.start();
mStartBtn.setText("关闭");
}
}
});

}

@Override
public void onDestroy(){
if (mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.stop();
mLocationClient = null;
}
super.onDestroy();
}
}
接下来看一下 activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="开始" />
</RelativeLayout>

</LinearLayout>
最后根据百度文档提出的权限问题,我们加到  AndroidMainFest.xml 中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationmanager"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />

<permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</permission>

<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
</uses-permission>
<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>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.locationmanager.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:permission="android.permission.BAIDU_LOCATION_SERVICE"
android:process=":remote" >
<intent-filter>
<action android:name="com.baidu.location.service_v2.6" >
</action>
</intent-filter>
</service>
</application>

</manifest>
运行后,就可以得到相应的位置信息了。同时我们还可以使用百度提供的其它服务,如地图标注功能等。

你可能感兴趣的:(Android获取当前位置,推荐使用百度地图SDK获取位置)