精华内容
下载资源
问答
  • Android获取地理位置

    2012-10-17 16:32:11
    采用异步加载,提高获取位置的性能,避免了使用gtLoastKnowLocation()获取不到地理位置的问题
  • } } /** * 通过经纬度得到地理位置 */ public static void getLocalPositionByAddress( AppActivity activity ,double lng,double lat) { String coordinate; String addressStr = ""; String sheng...

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

    }

    });

    }

    }

    展开全文
  • 安卓给我们提供了一个LocationManager.java类(文件位于framework/base/locaiton/java/android/location/LocationManager.java)我们可以直接使用,使用方法如下:private Location getLocation(){android.location....

    安卓给我们提供了一个LocationManager.java类(文件位于framework/base/locaiton/java/android/location/LocationManager.java)

    我们可以直接使用,使用方法如下:

    private Location getLocation(){

    android.location.LocationManager locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Location [] locations = new Location[2];

    String providerGPS = android.location.LocationManager.GPS_PROVIDER;

    String providerNetwork = android.location.LocationManager.NETWORK_PROVIDER;

    locations[0] = locationManager.getLastKnownLocation(providerGPS);

    locations[1] = locationManager.getLastKnownLocation(providerNetwork);

    Location currentLocation = null;

    for (int i = 0; i < locations.length; i++) {

    Location l = locations[i];

    if (l != null) return l;

    }

    return null;

    }

    这里我们同时使用GPS和Network来定位,优先使用GPS,当无法使用GPS获取时,使用Network定位。两者都获取不到地理位置信息时,返回null

    展开全文
  • 复制到libs下 附代码 ...static BDLocation lastLocation = null;...public MyLocationListenner myListener = new MyLocationListenner();...https://blog.csdn.net/meixi_android/article/details/84955589

    下载jar https://pan.baidu.com/s/1ioZ5_24WvsmMYxIcgoLyvA

    复制到libs下

    附代码

    static BDLocation lastLocation = null;
    private LocationClient mLocClient;
    public MyLocationListenner myListener = new MyLocationListenner();
        public class MyLocationListenner implements BDLocationListener {
            @Override
            public void onReceiveLocation(BDLocation location) {
                if (location == null) {
                    return;
                }
                Log.d("map", "On location change received:" + location);
                Log.d("map", "addr:" + location.getAddrStr());
    
                if (lastLocation != null) {
                    if (lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location.getLongitude()) {
                        Log.d("map", "same location, skip refresh");
                        // mMapView.refresh(); //need this refresh?
                        return;
                    }
                }
                String addrlg; //定位结果
                lastLocation = location;
                if (!TextUtils.isEmpty(lastLocation.getLocationDescribe())){
                    addrlg = lastLocation.getLocationDescribe();
                }else if (lastLocation.hasAddr()) {
                    addrlg = lastLocation.getAddrStr();
                }else {
                    addrlg = "定位失败...";
                    return;
                }
                String city = lastLocation.getCity();
                double lat = lastLocation.getLatitude();
                double lot = lastLocation.getLongitude();
                ShareUtil.sharedPstring("nowla",String.valueOf(lat));
                ShareUtil.sharedPstring("nowlo",String.valueOf(lot));
                Log.i("lgq","............"+addrlg+"........"+lat+"......."+lot);
    //            tv_bottom_bar_me.setText(addrlg);
    //            mBaiduMap.animateMapStatus(u);
            }
        }
    private void showMapWithLocationClient() {
    
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);
    
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);// open gps
        // option.setCoorType("bd09ll");
        // Johnson change to use gcj02 coordination. chinese national standard
        // so need to conver to bd09 everytime when draw on baidu map
        option.setCoorType("gcj02");
        option.setScanSpan(30000);
        option.setAddrType("all");
        mLocClient.setLocOption(option);
    
        mLocClient.start();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showMapWithLocationClient();
    
    }



    谷歌API也可以实现啦:https://blog.csdn.net/meixi_android/article/details/84955589

    展开全文
  •   移动应用可以通过GPS或者网络的方式获取当前位置信息,一般来说,通过GPS获得地理位置... 想获取地理位置信息,首先要在AndroidManifest.xml中加入获取地理位置信息的权限,(ACCESS_FINE_LOCATION允许应用从网...
     
    移动应用可以通过GPS或者网络的方式获取当前位置信息,一般来说,通过GPS获得地理位置往往精确度较高,但是在通信条件不好的地方可能获取不到,而通过网络信息获取地理位置精度低于GPS,但是只要网络通畅,即可保证能获取到地理位置信息
    想获取地理位置信息,首先要在AndroidManifest.xml中加入获取地理位置信息的权限,(ACCESS_FINE_LOCATION允许应用从网络信息中获取地理位置,ACCESS_COARSE_LOCATION允许应用从GPS中获取地理位置)
    uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
    uses-permission  android:name="android.permission.ACCESS_COARSE_LOCATION"
     
    获取地理位置信息的相关方法均被封装在android.location包中,常用的方法有两个,getLastLocation和requestLocationUpdates,前者获取上一次进行定位的地理位置,后者监听地理位置的变化
    getLastLocation:
    LocationManager locManger = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            if(!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            /*提示用户打开允许获得地理位置信息选项*/
            } 
            //使用GPS获取地理位置
            Location loc = locManger.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            //使用网络信息获取地理位置
            Location loc = locManger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
     
    requestLocationUpdates:
     LocationManager manager = (LocationManager) getApp().getSystemService(Context.LOCATION_SERVICE);
            if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {            //或者LocationManager.GPS_PROVIDER
                manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {    //或者GPS_PROVIDER
                    @Override
                    public void onLocationChanged(Location location) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }
    
                    @Override
                    public void onStatusChanged(String s, int i, Bundle bundle) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }
    
                    @Override
                    public void onProviderEnabled(String s) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }
    
                    @Override
                    public void onProviderDisabled(String s) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }
                });
            }
    
     
    展开全文
  • android 获取地理位置时为null

    千次阅读 2017-03-16 15:51:40
    发现有很多android 手机获取地理位置,很多时候都不一定能够获取到,并且不一定准确,因为那个位置都是上次获取的时候拿到的,得更新。具体实现代码如下import android.app.Activity; import android.content.pm....
  • } else if (mContext.checkPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION, pid, uid) == PERMISSION_GRANTED) { return RESOLUTION_LEVEL_COARSE; } else { return RESOLUTION_LEVEL_NONE; ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 13,384
精华内容 5,353
关键字:

安卓获取地理位置