android获取地理位置权限,安卓端获取地理位置
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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.get Context(), android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(Cocos2dxActivity.g etContext(), android.Manifest.permission.ACCESS_COARSE_LOC ATION) != PackageManager.PERMISSION_GRANTED) {
return '{err:'未开启定位'}';
}
LocationManager mLocationManager = (LocationManager) Cocos2dxActivity.getContext().getSystemService(Cocos2dxActivi ty.getContext().LOCATION_SERVICE);
List providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(pro vider);
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.getC ontext());
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.getA ddressLine(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+ ','+ro ad+ ');';
Cocos2dxJavascriptJavaBridge.evalString(tocode);
}
});
return '';
} else {
return '{err:'位置不可知'}';
}
}
/**
* 通过经纬度得到地理位置
*/
public static void getLocalPositionByAddress( AppActivity a ctivity ,double lng,double lat)
{
String coordinate;
String addressStr = '';
String sheng = ''; //省
String shi = ''; //市
String qu = ''; //区
String lu = ''; //路
Geocoder geocoder = new Geocoder(Cocos2dxActivity.getC ontext());
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.getA ddressLine(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+ ','+distr ict+ ','+road+ ');';
Cocos2dxJavascriptJavaBridge.evalString(tocode);
}
});
}
}。