android腾讯网络定位,实现android基于腾讯的定位并判断网络连接

本文介绍如何在Android应用中实现基于腾讯地图的定位服务,并在定位前进行网络状态判断,确保程序稳定运行。首先,需要导入腾讯定位的jar包并配置AndroidManifest.xml文件,包括设置权限和添加腾讯定位的key。然后,创建一个工具类用于检查网络状态。当网络可用时,启动定位服务,通过TencentLocationListener监听定位结果,展示地理位置信息。如果定位失败或网络不可用,将显示相应提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现android基于腾讯的定位并判断网络连接因为定位需要网络,所以如果不判断网络容易引起程序崩溃所以在定位前需要判断网络连接

本例子的源代码已上传我的资源页

http://download.youkuaiyun.com/detail/u012373815/9002539

主要步骤:

1.    导入腾讯定位所需jar包

2.    配置androidmanifest.xml权限 并添加腾讯定位的key

3.编写判断网络状态工具类

4.实现判断网络并定位

一,    导入腾讯定位所需jar包

758a2c40e469bc2e028e6dcb9057d65d.png

jar 包在我的源代码中有

二,    配置androidmanifest.xml权限 并添加腾讯定位的key

androidmanifest.xml权限:

腾讯定位的key 需要添加在之后之前

android:name="TencentMapSDK"

android:value="DUSBZ-LL63F-FOIJI-J2GW4-TWNFT-FEFBB"/>

三,    编写判断网络状态工具类

import android.app.Activity;

import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

public classPanduanNet {

/**

* 2015.5.18判断网络yang

*

*/

public static boolean detect(Activity act) {

ConnectivityManagermanager = (ConnectivityManager) act

.getApplicationContext().getSystemService(

Context.CONNECTIVITY_SERVICE);

if (manager == null) {

return false;

}

NetworkInfonetworkinfo = manager.getActiveNetworkInfo();

if (networkinfo == null ||!networkinfo.isAvailable()) {

return false;

}

return true;

}

}

四,    实现判断网络并定位

public classMainActivity extends Activity implements TencentLocationListener {

private TextView locationtext;

private TencentLocationManager locationManager;

private TencentLocationRequest request;

@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

locationtext=(TextView)findViewById(R.id.location);

boolean networkState=PanduanNet.detect(MainActivity.this);

if(!networkState)//判断网络

{

Toast.makeText(this, "定位失败,请连接网络", Toast.LENGTH_SHORT).show();

}else{

locationManager =TencentLocationManager.getInstance(this);

locationManager          .setCoordinateType(TencentLocationManager.COORDINATE_TYPE_GCJ02);

request = TencentLocationRequest.create();

request.setInterval(3000);

request.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_ADMIN_AREA);

int error = locationManager.requestLocationUpdates(request, this);

if (error == 0) {

System.out.println("地址获取成功");

}

}

}

@Override

public voidonLocationChanged(TencentLocation location, int error, String reason) {

//TODOAuto-generated method stub

//定位成功,获取城市,街区地址

if (error ==TencentLocation.ERROR_OK) {

Log.d("location",""+ location);

Stringaddress = location.getCity().toString()

+location.getTown().toString();

if(location.getStreet().toString().equals("Unknown")) {

//未得到街道地址

locationtext.setText(address);

}else{

//得到街道地址

locationtext.setText(address+ location.getStreet().toString());

}

Toast.makeText(MainActivity.this, "当前位置为" + address + "!!返回可以重新定位",

Toast.LENGTH_SHORT).show();

//停止定位

stopLocation();

}else{

//停止定位

stopLocation();

//定位失败

Toast.makeText(this, "定位失败,请连接网络", Toast.LENGTH_SHORT)

.show();

}

}

@Override

public void onStatusUpdate(Stringarg0, intarg1, String arg2) {

}

public void stopLocation() {

locationManager.removeUpdates(this);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值