Android 定位

依赖

implementation 'com.pubnub:pubnub-gson:4.20.0'
implementation 'org.xutils:xutils:3.3.36'

Android 定位_第1张图片

 

//MyApp

package com.example.admin.location.app;

import android.app.Application;

import org.xutils.x;


public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(false); //输出debug日志,开启会影响性能
    }
}

Android 定位_第2张图片

 

//需要的权限





//CityBean

package com.example.admin.location.bean;

import java.util.List;

public class CityBean{


    /**
     * status : 0
     * result : {"location":{"lng":116.40073899999994,"lat":39.922213076463244},"formatted_address":"北京市东城区西华门大街","business":"天安门,前门,和平门","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"北京市","city":"北京市","city_level":2,"district":"东城区","town":"","adcode":"110101","street":"西华门大街","street_number":"","direction":"","distance":""},"pois":[{"addr":"北京市东城区景山前街4号","cp":"","direction":"内","distance":"0","name":"故宫博物院","poiType":"旅游景点","point":{"x":116.40340711612929,"y":39.92409081387667},"tag":"旅游景点;博物馆","tel":"","uid":"06d2dffda107b0ef89f15db6","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0,"y":0},"direction":"","distance":"","uid":""}},{"addr":"景山前街4号故宫博物院内","cp":" ","direction":"东北","distance":"238","name":"故宫博物院-西门","poiType":"出入口","point":{"x":116.39909524968138,"y":39.92115808056728},"tag":"出入口;门","tel":"","uid":"7cf350461fb8abc0e9f85361","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市东城区景山前街4号故宫博物院内","cp":" ","direction":"东北","distance":"215","name":"西华门","poiType":"旅游景点","point":{"x":116.39913118190178,"y":39.92138634029797},"tag":"旅游景点;文物古迹","tel":"","uid":"d6c5d991f0c67ae830169bd1","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市东城区景山前街4号故宫博物院内","cp":" ","direction":"西南","distance":"412","name":"中和殿","poiType":"旅游景点","point":{"x":116.40338016696398,"y":39.92421531390408},"tag":"旅游景点;文物古迹","tel":"","uid":"a7e11ff29d1ac95879dbd26c","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市东城区景山前街4号故宫博物院内","cp":" ","direction":"西南","distance":"363","name":"太和殿","poiType":"旅游景点","point":{"x":116.40342508223948,"y":39.9236412285445},"tag":"旅游景点;文物古迹","tel":"","uid":"69d6bc5b6f6c5163d92f2882","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市东城区景山前街4号故宫博物院内","cp":" ","direction":"西北","distance":"379","name":"内金水桥","poiType":"旅游景点","point":{"x":116.40357779417617,"y":39.92075689493951},"tag":"旅游景点;文物古迹","tel":"","uid":"56e88cba1fd9f765a4c7567b","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市西城区北长街113号","cp":" ","direction":"东","distance":"305","name":"北京一六一中学(北校区)","poiType":"教育培训","point":{"x":116.39802626612452,"y":39.922548377944466},"tag":"教育培训;中学","tel":"","uid":"e467141ecf6544701994c6a7","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0,"y":0},"direction":"","distance":"","uid":""}},{"addr":"西城区南长街20号","cp":" ","direction":"东北","distance":"407","name":"泽园酒家(南长街店)","poiType":"美食","point":{"x":116.39804423223471,"y":39.920300370481044},"tag":"美食;中餐厅","tel":"","uid":"f4a33da9b05afa103010af13","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0,"y":0},"direction":"","distance":"","uid":""}},{"addr":"南长街与西华门大街交叉口西北150米","cp":" ","direction":"东","distance":"454","name":"西苑门","poiType":"房地产","point":{"x":116.39668779091464,"y":39.92184977436147},"tag":"房地产;住宅区","tel":"","uid":"17cd08ff5d2c85ed6ea3cc92","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0,"y":0},"direction":"","distance":"","uid":""}},{"addr":"北京市东城区中华路4号","cp":" ","direction":"北","distance":"462","name":"中山公园-游船码头","poiType":"交通设施","point":{"x":116.39961626687717,"y":39.919131377200166},"tag":"交通设施;港口","tel":"","uid":"7de6b58f3ea0386e3517dc90","zip":"","parent_poi":{"name":"中山公园","tag":"旅游景点;风景区","addr":"北京市东城区中华路4号","point":{"x":116.40057745377285,"y":39.91669648637688},"direction":"北","distance":"797","uid":"e2c734e489d8d15d15a190d1"}}],"roads":[],"poiRegions":[{"direction_desc":"内","name":"故宫博物院","tag":"旅游景点;博物馆","uid":"06d2dffda107b0ef89f15db6"}],"sematic_description":"故宫博物院内,西华门东北215米","cityCode":131}
     */

    private int status;
    private ResultBean result;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public ResultBean getResult() {
        return result;
    }

    public void setResult(ResultBean result) {
        this.result = result;
    }

    public static class ResultBean {
        /**
         * location : {"lng":116.40073899999994,"lat":39.922213076463244}
         * formatted_address : 北京市东城区西华门大街
         * business : 天安门,前门,和平门
         * addressComponent : {"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"北京市","city":"北京市","city_level":2,"district":"东城区","town":"","adcode":"110101","street":"西华门大街","street_number":"","direction":"","distance":""}
         * pois : [{"addr":"北京市东城区景山前街4号","cp":"","direction":"内","distance":"0","name":"故宫博物院","poiType":"旅游景点","point":{"x":116.40340711612929,"y":39.92409081387667},"tag":"旅游景点;博物馆","tel":"","uid":"06d2dffda107b0ef89f15db6","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0,"y":0},"direction":"","distance":"","uid":""}},{"addr":"景山前街4号故宫博物院内","cp":" ","direction":"东北","distance":"238","name":"故宫博物院-西门","poiType":"出入口","point":{"x":116.39909524968138,"y":39.92115808056728},"tag":"出入口;门","tel":"","uid":"7cf350461fb8abc0e9f85361","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市东城区景山前街4号故宫博物院内","cp":" ","direction":"东北","distance":"215","name":"西华门","poiType":"旅游景点","point":{"x":116.39913118190178,"y":39.92138634029797},"tag":"旅游景点;文物古迹","tel":"","uid":"d6c5d991f0c67ae830169bd1","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市东城区景山前街4号故宫博物院内","cp":" ","direction":"西南","distance":"412","name":"中和殿","poiType":"旅游景点","point":{"x":116.40338016696398,"y":39.92421531390408},"tag":"旅游景点;文物古迹","tel":"","uid":"a7e11ff29d1ac95879dbd26c","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市东城区景山前街4号故宫博物院内","cp":" ","direction":"西南","distance":"363","name":"太和殿","poiType":"旅游景点","point":{"x":116.40342508223948,"y":39.9236412285445},"tag":"旅游景点;文物古迹","tel":"","uid":"69d6bc5b6f6c5163d92f2882","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市东城区景山前街4号故宫博物院内","cp":" ","direction":"西北","distance":"379","name":"内金水桥","poiType":"旅游景点","point":{"x":116.40357779417617,"y":39.92075689493951},"tag":"旅游景点;文物古迹","tel":"","uid":"56e88cba1fd9f765a4c7567b","zip":"","parent_poi":{"name":"故宫博物院","tag":"旅游景点;博物馆","addr":"北京市东城区景山前街4号","point":{"x":116.40340711612929,"y":39.92409081387667},"direction":"西南","distance":"402","uid":"06d2dffda107b0ef89f15db6"}},{"addr":"北京市西城区北长街113号","cp":" ","direction":"东","distance":"305","name":"北京一六一中学(北校区)","poiType":"教育培训","point":{"x":116.39802626612452,"y":39.922548377944466},"tag":"教育培训;中学","tel":"","uid":"e467141ecf6544701994c6a7","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0,"y":0},"direction":"","distance":"","uid":""}},{"addr":"西城区南长街20号","cp":" ","direction":"东北","distance":"407","name":"泽园酒家(南长街店)","poiType":"美食","point":{"x":116.39804423223471,"y":39.920300370481044},"tag":"美食;中餐厅","tel":"","uid":"f4a33da9b05afa103010af13","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0,"y":0},"direction":"","distance":"","uid":""}},{"addr":"南长街与西华门大街交叉口西北150米","cp":" ","direction":"东","distance":"454","name":"西苑门","poiType":"房地产","point":{"x":116.39668779091464,"y":39.92184977436147},"tag":"房地产;住宅区","tel":"","uid":"17cd08ff5d2c85ed6ea3cc92","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0,"y":0},"direction":"","distance":"","uid":""}},{"addr":"北京市东城区中华路4号","cp":" ","direction":"北","distance":"462","name":"中山公园-游船码头","poiType":"交通设施","point":{"x":116.39961626687717,"y":39.919131377200166},"tag":"交通设施;港口","tel":"","uid":"7de6b58f3ea0386e3517dc90","zip":"","parent_poi":{"name":"中山公园","tag":"旅游景点;风景区","addr":"北京市东城区中华路4号","point":{"x":116.40057745377285,"y":39.91669648637688},"direction":"北","distance":"797","uid":"e2c734e489d8d15d15a190d1"}}]
         * roads : []
         * poiRegions : [{"direction_desc":"内","name":"故宫博物院","tag":"旅游景点;博物馆","uid":"06d2dffda107b0ef89f15db6"}]
         * sematic_description : 故宫博物院内,西华门东北215米
         * cityCode : 131
         */

        private LocationBean location;
        private String formatted_address;
        private String business;
        private AddressComponentBean addressComponent;
        private String sematic_description;
        private int cityCode;
        private List pois;
        private List roads;
        private List poiRegions;

        public LocationBean getLocation() {
            return location;
        }

        public void setLocation(LocationBean location) {
            this.location = location;
        }

        public String getFormatted_address() {
            return formatted_address;
        }

        public void setFormatted_address(String formatted_address) {
            this.formatted_address = formatted_address;
        }

        public String getBusiness() {
            return business;
        }

        public void setBusiness(String business) {
            this.business = business;
        }

        public AddressComponentBean getAddressComponent() {
            return addressComponent;
        }

        public void setAddressComponent(AddressComponentBean addressComponent) {
            this.addressComponent = addressComponent;
        }

        public String getSematic_description() {
            return sematic_description;
        }

        public void setSematic_description(String sematic_description) {
            this.sematic_description = sematic_description;
        }

        public int getCityCode() {
            return cityCode;
        }

        public void setCityCode(int cityCode) {
            this.cityCode = cityCode;
        }

        public List getPois() {
            return pois;
        }

        public void setPois(List pois) {
            this.pois = pois;
        }

        public List getRoads() {
            return roads;
        }

        public void setRoads(List roads) {
            this.roads = roads;
        }

        public List getPoiRegions() {
            return poiRegions;
        }

        public void setPoiRegions(List poiRegions) {
            this.poiRegions = poiRegions;
        }

        public static class LocationBean {
            /**
             * lng : 116.40073899999994
             * lat : 39.922213076463244
             */

            private double lng;
            private double lat;

            public double getLng() {
                return lng;
            }

            public void setLng(double lng) {
                this.lng = lng;
            }

            public double getLat() {
                return lat;
            }

            public void setLat(double lat) {
                this.lat = lat;
            }
        }

        public static class AddressComponentBean {
            /**
             * country : 中国
             * country_code : 0
             * country_code_iso : CHN
             * country_code_iso2 : CN
             * province : 北京市
             * city : 北京市
             * city_level : 2
             * district : 东城区
             * town :
             * adcode : 110101
             * street : 西华门大街
             * street_number :
             * direction :
             * distance :
             */

            private String country;
            private int country_code;
            private String country_code_iso;
            private String country_code_iso2;
            private String province;
            private String city;
            private int city_level;
            private String district;
            private String town;
            private String adcode;
            private String street;
            private String street_number;
            private String direction;
            private String distance;

            public String getCountry() {
                return country;
            }

            public void setCountry(String country) {
                this.country = country;
            }

            public int getCountry_code() {
                return country_code;
            }

            public void setCountry_code(int country_code) {
                this.country_code = country_code;
            }

            public String getCountry_code_iso() {
                return country_code_iso;
            }

            public void setCountry_code_iso(String country_code_iso) {
                this.country_code_iso = country_code_iso;
            }

            public String getCountry_code_iso2() {
                return country_code_iso2;
            }

            public void setCountry_code_iso2(String country_code_iso2) {
                this.country_code_iso2 = country_code_iso2;
            }

            public String getProvince() {
                return province;
            }

            public void setProvince(String province) {
                this.province = province;
            }

            public String getCity() {
                return city;
            }

            public void setCity(String city) {
                this.city = city;
            }

            public int getCity_level() {
                return city_level;
            }

            public void setCity_level(int city_level) {
                this.city_level = city_level;
            }

            public String getDistrict() {
                return district;
            }

            public void setDistrict(String district) {
                this.district = district;
            }

            public String getTown() {
                return town;
            }

            public void setTown(String town) {
                this.town = town;
            }

            public String getAdcode() {
                return adcode;
            }

            public void setAdcode(String adcode) {
                this.adcode = adcode;
            }

            public String getStreet() {
                return street;
            }

            public void setStreet(String street) {
                this.street = street;
            }

            public String getStreet_number() {
                return street_number;
            }

            public void setStreet_number(String street_number) {
                this.street_number = street_number;
            }

            public String getDirection() {
                return direction;
            }

            public void setDirection(String direction) {
                this.direction = direction;
            }

            public String getDistance() {
                return distance;
            }

            public void setDistance(String distance) {
                this.distance = distance;
            }
        }

        public static class PoisBean {
            /**
             * addr : 北京市东城区景山前街4号
             * cp :
             * direction : 内
             * distance : 0
             * name : 故宫博物院
             * poiType : 旅游景点
             * point : {"x":116.40340711612929,"y":39.92409081387667}
             * tag : 旅游景点;博物馆
             * tel :
             * uid : 06d2dffda107b0ef89f15db6
             * zip :
             * parent_poi : {"name":"","tag":"","addr":"","point":{"x":0,"y":0},"direction":"","distance":"","uid":""}
             */

            private String addr;
            private String cp;
            private String direction;
            private String distance;
            private String name;
            private String poiType;
            private PointBean point;
            private String tag;
            private String tel;
            private String uid;
            private String zip;
            private ParentPoiBean parent_poi;

            public String getAddr() {
                return addr;
            }

            public void setAddr(String addr) {
                this.addr = addr;
            }

            public String getCp() {
                return cp;
            }

            public void setCp(String cp) {
                this.cp = cp;
            }

            public String getDirection() {
                return direction;
            }

            public void setDirection(String direction) {
                this.direction = direction;
            }

            public String getDistance() {
                return distance;
            }

            public void setDistance(String distance) {
                this.distance = distance;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getPoiType() {
                return poiType;
            }

            public void setPoiType(String poiType) {
                this.poiType = poiType;
            }

            public PointBean getPoint() {
                return point;
            }

            public void setPoint(PointBean point) {
                this.point = point;
            }

            public String getTag() {
                return tag;
            }

            public void setTag(String tag) {
                this.tag = tag;
            }

            public String getTel() {
                return tel;
            }

            public void setTel(String tel) {
                this.tel = tel;
            }

            public String getUid() {
                return uid;
            }

            public void setUid(String uid) {
                this.uid = uid;
            }

            public String getZip() {
                return zip;
            }

            public void setZip(String zip) {
                this.zip = zip;
            }

            public ParentPoiBean getParent_poi() {
                return parent_poi;
            }

            public void setParent_poi(ParentPoiBean parent_poi) {
                this.parent_poi = parent_poi;
            }

            public static class PointBean {
                /**
                 * x : 116.40340711612929
                 * y : 39.92409081387667
                 */

                private double x;
                private double y;

                public double getX() {
                    return x;
                }

                public void setX(double x) {
                    this.x = x;
                }

                public double getY() {
                    return y;
                }

                public void setY(double y) {
                    this.y = y;
                }
            }

            public static class ParentPoiBean {
                /**
                 * name :
                 * tag :
                 * addr :
                 * point : {"x":0,"y":0}
                 * direction :
                 * distance :
                 * uid :
                 */

                private String name;
                private String tag;
                private String addr;
                private PointBeanX point;
                private String direction;
                private String distance;
                private String uid;

                public String getName() {
                    return name;
                }

                public void setName(String name) {
                    this.name = name;
                }

                public String getTag() {
                    return tag;
                }

                public void setTag(String tag) {
                    this.tag = tag;
                }

                public String getAddr() {
                    return addr;
                }

                public void setAddr(String addr) {
                    this.addr = addr;
                }

                public PointBeanX getPoint() {
                    return point;
                }

                public void setPoint(PointBeanX point) {
                    this.point = point;
                }

                public String getDirection() {
                    return direction;
                }

                public void setDirection(String direction) {
                    this.direction = direction;
                }

                public String getDistance() {
                    return distance;
                }

                public void setDistance(String distance) {
                    this.distance = distance;
                }

                public String getUid() {
                    return uid;
                }

                public void setUid(String uid) {
                    this.uid = uid;
                }

                public static class PointBeanX {
                    /**
                     * x : 0.0
                     * y : 0.0
                     */

                    private double x;
                    private double y;

                    public double getX() {
                        return x;
                    }

                    public void setX(double x) {
                        this.x = x;
                    }

                    public double getY() {
                        return y;
                    }

                    public void setY(double y) {
                        this.y = y;
                    }
                }
            }
        }

        public static class PoiRegionsBean {
            /**
             * direction_desc : 内
             * name : 故宫博物院
             * tag : 旅游景点;博物馆
             * uid : 06d2dffda107b0ef89f15db6
             */

            private String direction_desc;
            private String name;
            private String tag;
            private String uid;

            public String getDirection_desc() {
                return direction_desc;
            }

            public void setDirection_desc(String direction_desc) {
                this.direction_desc = direction_desc;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getTag() {
                return tag;
            }

            public void setTag(String tag) {
                this.tag = tag;
            }

            public String getUid() {
                return uid;
            }

            public void setUid(String uid) {
                this.uid = uid;
            }
        }
    }
}

//MainActivity

 

package com.example.admin.location;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.admin.location.bean.CityBean;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private LocationManager locationManager;
    private String locationProvider;
    private TextView location_city;

    LocationListener locationListener=new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //经纬度
        location_city = findViewById(R.id.location_city);
        location();

    }

    private void location() {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String locationstring = judgeProvider(lm);
        Log.d("MainActivity_aaaa", locationstring);
        //获取地理位置管理器
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //获取所有可用的位置提供器
        List providers = locationManager.getProviders(true);
        if (providers.contains(LocationManager.GPS_PROVIDER)) {
            //如果是GPS
            locationProvider = LocationManager.GPS_PROVIDER;
        } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            //如果是Network
            locationProvider = LocationManager.NETWORK_PROVIDER;
        } else {
            Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
            return;
        }
        Location location = locationManager.getLastKnownLocation(locationProvider);
        if (location != null) {
            //获取当前位置,这里只用到了经纬度
            String stringa = "纬度为:" + location.getLatitude() + ",经度为:"
                    + location.getLongitude();
            Log.d("MainActivity_aaaa", "纬度为:" + location.getLatitude());
            Log.d("MainActivity_aaaa", "经度为:" + location.getLongitude());
            RequestParams params = new RequestParams("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + location.getLatitude() + "," + location.getLongitude() + "&output=json&pois=1&ak=LFx9XKQxYKsBkKR4syBfRLy3P0zNHs1e");
            x.http().get(params, new Callback.CommonCallback() {
                @Override
                public void onSuccess(String result) {        //解析result
                    Log.d("MainActivity_aaaa", result);

                    result = result.substring(29, result.length() - 1);
                    Log.d("MainActivity_aaaa", "=" + result);
                    Gson gson = new Gson();
                    CityBean cityBean = gson.fromJson(result, CityBean.class);
                    String formatted_address = cityBean.getResult().getAddressComponent().getCity();
                    Log.d("MainActivity_aaaa", "formatted_address:" + formatted_address);
                    location_city.setText(formatted_address);

                }

                //请求异常后的回调方法
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    Toast.makeText(MainActivity.this, "位置出现错误", Toast.LENGTH_SHORT).show();
                }

                //主动调用取消请求的回调方法
                @Override
                public void onCancelled(CancelledException cex) {
                }

                @Override
                public void onFinished() {
                }
            });
        }
        locationManager.requestLocationUpdates(locationProvider, 2000, 2, locationListener);

        // http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=30.68093376455154,104.06552381979525&output=json&pois=1&ak=LFx9XKQxYKsBkKR4syBfRLy3P0zNHs1e
        // http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=39.922213,116.400739&output=json&pois=1&ak=LFx9XKQxYKsBkKR4syBfRLy3P0zNHs1e

    }


    private String judgeProvider(LocationManager locationManager) {
        List prodiverlist = locationManager.getProviders(true);
        if (prodiverlist.contains(LocationManager.NETWORK_PROVIDER)) {
            return LocationManager.NETWORK_PROVIDER;//网络定位
        } else if (prodiverlist.contains(LocationManager.GPS_PROVIDER)) {
            return LocationManager.GPS_PROVIDER;//GPS定位
        } else {
            Toast.makeText(MainActivity.this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
        }
        return null;
    }


}

//界面布局

activity_main.xml



    
    

 

 

你可能感兴趣的:(Andriod,定位)