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<String>?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()?<?bestLocation.getAccuracy())?{
????????????????//?Found?best?last?known?location:?%s",?l);
????????????????bestLocation?=?l;
????????????}
????????}
????????if?(bestLocation?!=?null)?{
????????????String?coordinate;
????????????String?addressStr?=?"";
????????????String?sheng?=?"";????????//省
????????????String?shi?=?"";????????//市
????????????String?qu?=?"";????????//區(qū)
????????????String?lu?=?"";????????//路
????????????final?double?longitude?=?bestLocation.getLongitude();?//經(jīng)度
????????????final?double?latitude?=?bestLocation.getLatitude();??//維度
????????????Geocoder?geocoder?=?new?Geocoder(Cocos2dxActivity.getContext());
????????????try?{
????????????????List<Address>?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;//詳細(xì)位置:**省**市**區(qū)**街道**小區(qū)**號
????????????final?String?province?=?sheng;//省
????????????final?String?city?=?shi;????//市
????????????final?String?district?=?qu;?//區(qū)
????????????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:'位置不可知'}";
????????}
????}
????/**
?????*?通過經(jīng)緯度得到地理位置
?????*/
????public?static?void?getLocalPositionByAddress(?AppActivity?activity?,double?lng,double?lat)
????{
????????String?coordinate;
????????String?addressStr?=?"";
????????String?sheng?=?"";????????//省
????????String?shi?=?"";????????//市
????????String?qu?=?"";????????//區(qū)
????????String?lu?=?"";????????//路
????????Geocoder?geocoder?=?new?Geocoder(Cocos2dxActivity.getContext());
????????try?{
????????????List<Address>?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;//詳細(xì)位置:**省**市**區(qū)**街道**小區(qū)**號
????????final?String?province?=?sheng;//省
????????final?String?city?=?shi;????//市
????????final?String?district?=?qu;?//區(qū)
????????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);
????????????}
????????});
????}
}