Location




public void getLocation() {
//获取Location
if (ActivityCompat.checkSelfPermission(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;
}

String locationProvider; //获取地理位置管理器 LocationManager 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) { //不为空,显示地理位置经纬度 updateVersion(location.getLatitude(), location.getLongitude()); } //监视地理位置变化 locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener); }

【Location】///////////////////////////////////////////
LocationListener locationListener = new LocationListener() {
@Override public void onStatusChanged(String provider, int status, Bundle arg2) {}@Override public void onProviderEnabled(String provider) {}@Override public void onProviderDisabled(String provider) {}@Override public void onLocationChanged(Location location) { //如果位置发生变化,重新显示 updateVersion(location.getLatitude(), location.getLongitude()); } };

////////////////////////////////////////////////////////////////////////////////
public void updateVersion(double wd, double jd) {
final String path = "http://api.map.baidu.com/geocoder?output=json&location=" + wd + "," + jd;
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL(path);
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int code=urlConnection.getResponseCode();
if (code==200) {
InputStream inputStream=urlConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer buffer=new StringBuffer();
while ((line=bufferedReader.readLine())!=null) {
buffer.append(line);
} final String str =buffer.toString(); runOnUiThread(new Runnable() { @Override public void run() { TextView textView = findViewById(R.id.tv_location); textView.setText(str); } }); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) {} } }).start(); }

###################################################
public void setTags(List tags) {
if (tags != null && tags.size() > 0) {
for (int i = 0; i < tags.size(); i++) {
TextView textView = new TextView(getContext());
textView.setSingleLine();
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setGravity(Gravity.CENTER);
textView.setTextSize(13);
textView.setTextColor(Color.RED);
textView.setText(tags.get(i));
LayoutParams params = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
addView(textView, params);
}
}
}
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int withLeft = 0; int width = getWidth(); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View view = getChildAt(i); int measuredWidth = view.getMeasuredWidth(); int measuredHeight = view.getMeasuredHeight(); if (withLeft + measuredWidth > width) { if (i == 0) { int withMeasureSpec = MeasureSpec.makeMeasureSpec(width - withLeft, MeasureSpec.EXACTLY); int heiMeasureSpec = MeasureSpec.makeMeasureSpec(measuredHeight, MeasureSpec.EXACTLY); view.measure(withMeasureSpec, heiMeasureSpec); view.layout(withLeft, 0, withLeft + view.getMeasuredWidth(), view.getMeasuredHeight()); } else { break; } } else { view.layout(withLeft + SPACE, 0, withLeft + SPACE + view.getMeasuredWidth(), view.getMeasuredHeight()); } withLeft += measuredWidth + SPACE; } }

    推荐阅读