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);

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值