腾讯位置服务:http://lbs.qq.com/
开发-> Android定位SDK。如下图所示。
下载到本地:
解压后
打开eclipse新建工程,选择合适的API:
新建工程目录如下:
导入第三方包:
添加驱动程序:
具体可以参考官方开发指南:http://lbs.qq.com/geo/guide-use.html
AndroidManifest.xml设置权限与密钥
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
package="com.hxy.location"
android:versionCode="1"
android:versionName="1.0" >
"android.permission.ACCESS_FINE_LOCATION" />
"android.permission.ACCESS_COARSE_LOCATION" />
"android.permission.INTERNET" />
"android.permission.ACCESS_WIFI_STATE" />
"android.permission.CHANGE_WIFI_STATE" />
"android.permission.ACCESS_NETWORK_STATE" />
"android.permission.CHANGE_NETWORK_STATE" />
"android.permission.READ_PHONE_STATE" />
"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
"android.permission.WAKE_LOCK" />
"android.permission.READ_PHONE_SINTERNETWIFI_STATE" />
"android.permission.CALL_PHONE" />
"android.permission.WRITE_EXTERNAL_STORAGE" />
"android.permission.VIBRATE" />
android:minSdkVersion="19"
android:targetSdkVersion="19" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.hxy.location.MainActivity"
android:label="@string/app_name" >
"android.intent.action.MAIN" />
"android.intent.category.LAUNCHER" />
android:name="TencentMapSDK"
android:value="EXLBZ-B7TH5-CTZIK-QNQFR-76OGO-GNFCS" />
MainActivity.java
package com.hxy.location;
import com.tencent.map.geolocation.*;
import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
public class MainActivity extends Activity implements TencentLocationListener{
private TextView mStatus;
private TencentLocation mLocation;
private TencentLocationManager mLocationManager;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStatus = (TextView) findViewById(R.id.mStatus);
mLocationManager = TencentLocationManager.getInstance(this);
mLocationManager.requestLocationUpdates(TencentLocationRequest.create().setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_NAME).setInterval(500).setAllowDirection(true), this);
}
public void onLocationChanged(TencentLocation location, int error,String reason) {
if (error == TencentLocation.ERROR_OK) {
//定位成功
mLocation = location;
StringBuilder sb = new StringBuilder();
sb.append("(纬度=").append(location.getLatitude()).append(",经度=").append(location.getLongitude()).append(",精度=").append(location.getAccuracy()).append("), 来源=").append(location.getProvider()).append(", 地址=").append(location.getAddress());
//更新 status
mStatus.setText(sb.toString());
}
else Toast.makeText(MainActivity.this, "定位失败", Toast.LENGTH_SHORT).show();
}
public void onStatusUpdate(String arg0, int arg1, String arg2) {
// TODO Auto-generated method stub
}
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mLocationManager.removeUpdates(this);
}
}
activity_main.xml
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/mStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
gf��g�n�?̼��
网友评论