android 获取地理位置权限,安卓端获取地理位置

package org.cocos2dx.javascript.LocaltionPos;

import android.content.pm.PackageManager;

import android.location.Address;

import android.location.Geocoder;

import android.location.Location;

import android.location.LocationManager;

import android.support.v4.app.ActivityCompat;

import org.cocos2dx.javascript.AppActivity;

import org.cocos2dx.lib.Cocos2dxActivity;

import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;

import java.io.IOException;

import java.util.List;

public class LocaltionPos {

public static String getLocation(AppActivity activity) {

if (ActivityCompat.checkSelfPermission(Cocos2dxActivity.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED

&& ActivityCompat.checkSelfPermission(Cocos2dxActivity.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)

{

return "{err:'未开启定位'}";

}

LocationManager mLocationManager = (LocationManager) Cocos2dxActivity.getContext().getSystemService(Cocos2dxActivity.getContext().LOCATION_SERVICE);

List providers = mLocationManager.getProviders(true);

Location bestLocation = null;

for (String provider : providers) {

Location l = mLocationManager.getLastKnownLocation(provider);

if (l == null) {

continue;

}

if (bestLocation == null || l.getAccuracy() 

// Found best last known location: %s", l);

bestLocation = l;

}

}

if (bestLocation != null) {

String coordinate;

String addressStr = "";

String sheng = "";        //省

String shi = "";        //市

String qu = "";        //区

String lu = "";        //路

final double longitude = bestLocation.getLongitude(); //经度

final double latitude = bestLocation.getLatitude();  //维度

Geocoder geocoder = new Geocoder(Cocos2dxActivity.getContext());

try {

List

 addresses = geocoder.getFromLocation(latitude, longitude,1);

StringBuilder sb = new StringBuilder();

if (addresses.size() > 0) {

Address address = addresses.get(0);

int maxLine = address.getMaxAddressLineIndex();

if (maxLine >= 2) {

addressStr = address.getAddressLine(0) + " " + address.getAddressLine(1);

} else {

addressStr = address.getAddressLine(0);

}

sheng = address.getAdminArea();

shi = address.getLocality();

qu = address.getSubLocality();

lu = address.getThoroughfare();

}

} catch (IOException e) {

e.printStackTrace();

}

final String pos = addressStr;//详细位置:**省**市**区**街道**小区**号

final String province = sheng;//省

final String city = shi;    //市

final String district = qu; //区

final String road = lu;     //路

activity.runOnGLThread(new Runnable() {

@Override

public void run() {

String tocode = "cc.game.emit(\"LocaltionPos\", "+latitude+","+longitude+","+ pos + ","+province+ ","+city+ ","+district+ ","+road+ ");";

Cocos2dxJavascriptJavaBridge.evalString(tocode);

}

});

return "";

} else {

return "{err:'位置不可知'}";

}

}

/**

* 通过经纬度得到地理位置

*/

public static void getLocalPositionByAddress( AppActivity activity ,double lng,double lat)

{

String coordinate;

String addressStr = "";

String sheng = "";        //省

String shi = "";        //市

String qu = "";        //区

String lu = "";        //路

Geocoder geocoder = new Geocoder(Cocos2dxActivity.getContext());

try {

List

 addresses = geocoder.getFromLocation(lat, lng,1);

StringBuilder sb = new StringBuilder();

if (addresses.size() > 0) {

Address address = addresses.get(0);

int maxLine = address.getMaxAddressLineIndex();

if (maxLine >= 2) {

addressStr = address.getAddressLine(0) + " " + address.getAddressLine(1);

} else {

addressStr = address.getAddressLine(0);

}

sheng = address.getAdminArea();

shi = address.getLocality();

qu = address.getSubLocality();

lu = address.getThoroughfare();

}

} catch (IOException e) {

e.printStackTrace();

}

final String pos = addressStr;//详细位置:**省**市**区**街道**小区**号

final String province = sheng;//省

final String city = shi;    //市

final String district = qu; //区

final String road = lu;     //路

final double longitude = lng;

final double latitude = lat;

activity.runOnGLThread(new Runnable() {

@Override

public void run() {

String tocode = "cc.game.emit(\"LocaltionPosByAddress\", "+latitude+","+longitude+","+ pos + ","+province+ ","+city+ ","+district+ ","+road+ ");";

Cocos2dxJavascriptJavaBridge.evalString(tocode);

}

});

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中获取应用程序的地理位置通常涉及使用Google Play Services库中的Location APIs,特别是FusedLocationProviderClient或LocationManager。以下是基本步骤: 1. **添加权限**: 在AndroidManifest.xml文件中,添加`<uses-permission>`标签以请求访问设备的位置信息: ```xml <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 2. **引入依赖**: 如果使用的是AndroidX,可以在build.gradle(Module)文件中引入: ```gradle implementation 'com.google.android.gms:play-services-location:18.0.0' ``` 或者如果你还在使用Support Library,用: ```gradle implementation 'com.google.android.gms:play-services-location:17.0.0' ``` 3. **创建Location请求管理器**: 使用FusedLocationProviderClient: ```java private FusedLocationProviderClient fusedLocationClient; protected void onCreate(Bundle savedInstanceState) { fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); } ``` 或使用LocationManager: ```java private LocationManager locationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); } ``` 4. **请求位置更新**: 选择一种方法(如周期性请求、一次性或根据需求)调用`requestLocationUpdates()`或类似方法。 5. **处理位置回调**: 实现`LocationCallback`或自定义监听器,并在onLocationChanged()方法中处理获取到的新位置数据。 6. **异常处理**: 处理可能发生的`LocationPermission Denied`或网络连接等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值