天天看点

用户位置定位

之前看到很多应用会自动检测到用户所在的城市,后来想了想原理,其实就是检测到经纬度,然后根据经纬度判断所在位置或城市。

用户位置定位

不过多介绍了,直接上马吧。

package com.bc.jy.capital.myapplicationsssss.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.TextView;

import com.bc.jy.capital.myapplicationsssss.R;
import com.bc.jy.capital.myapplicationsssss.entity.DingWei;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/4.
 */

public class DingweiAcitivity extends Activity {
    private static LocationManager locationManager;
    private static Activity mActivity;
    private Gson mGson=new Gson();
    //private MKSearch mMKSearch;
    String key = "";//我是用的是聚合的数据URL在下方,key就要你自己去聚合申请了
    String url;
    Location location;
    TextView textView;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dingwei);
        mActivity = DingweiAcitivity.this;
        context = DingweiAcitivity.this;
        textView = findViewById(R.id.tv_dingwei);
        getLocation();//首先判断经纬度
        double lat =  location.getLatitude();//纬度
        double lng = location.getLongitude();//精度
        url = "http://apis.juhe.cn/geo/?key="+key+"&lat="+lat+"&lng="+lng+"&type=1";//聚合url
        jiexi();//根据经纬度查询所在地区,具体要的内容可以看这个url返回的内容。
    }

    public void getLocation() {
        locationManager = (LocationManager) mActivity.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(DingweiAcitivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Log.i("TAG", "纬度:"+location.getLatitude());
        Log.i("TAG", "经度:"+location.getLongitude());
        Log.i("TAG", "海拔:"+location.getAltitude());
        Log.i("TAG", "时间:"+location.getTime());
    }
    private void jiexi(){//这个本来是使用post的,但是在上面我们就已经把需要的添加完成,所以就是用get
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.d("sss","response:::"+response);
                        if (response==null){
                            return;
                        }
                        DingWei touTiaoBean = mGson.fromJson(response, DingWei.class);
                        String result = touTiaoBean.getResult().getExt().getCity();获取的地址
                        String result1 = touTiaoBean.getResult().getBusiness();
                        int result2 = touTiaoBean.getResult().getCitycode();
                        String result3 = touTiaoBean.getResult().getType();
                        String result4 = touTiaoBean.getResult().getLat();
                        String result5 = touTiaoBean.getResult().getLng();
                        Log.d("sss","result:::"+result);
                        textView.setText(result);//地址只有一条,所以无需使用适配器。
            
}
                });
    }

}
           
解析我使用的就是依赖gson:
compile 'com.google.code.gson:gson:2.8.0'      
布局就更简单了,直接一个textview,显示内容就行了