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