Android获取当前位置,推荐使用百度地图SDK获取位置
生活随笔
收集整理的這篇文章主要介紹了
Android获取当前位置,推荐使用百度地图SDK获取位置
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這里直接引用了百度V2.6出給出的DEMO實(shí)例,經(jīng)過(guò)測(cè)試,在手機(jī)客戶端可以獲取到當(dāng)前所在的位置 首先先看一下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"); //設(shè)置坐標(biāo)類型為bd09ll
option.setPriority(LocationClientOption.NetWorkFirst); //設(shè)置網(wǎng)絡(luò)優(yōu)先
option.setProdName("locSDKDemo2"); //設(shè)置產(chǎn)品線名稱
option.setScanSpan(5000); //定時(shí)定位,每隔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("關(guān)閉");
}
}
});}@Override
public void onDestroy(){
if (mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.stop();
mLocationClient = null;
}
super.onDestroy();
}
}
接下來(lái)看一下 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>
最后根據(jù)百度文檔提出的權(quán)限問(wèn)題,我們加到 ?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>
運(yùn)行后,就可以得到相應(yīng)的位置信息了。同時(shí)我們還可以使用百度提供的其它服務(wù),如地圖標(biāo)注功能等。
總結(jié)
以上是生活随笔為你收集整理的Android获取当前位置,推荐使用百度地图SDK获取位置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android中获取当前位置的使用步骤
- 下一篇: Mysql命令行修改字段类型