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;
}
}
推荐阅读
- GCD小总结
- Mockito|Mockito mock void methods Example
- 第一个程序——helloworld
- C# 接口实例
- Location对象
- http://note.youdao.com/yws/public/redirect/share?id=8e5a812e5778216a0752f838e6b9ae37&type=false
- 算法笔记(1)|算法笔记(1)| 排序
- 正则表达式过滤掉数字广告
- 求一个分数2/1|求一个分数2/1, 3/2, 5/3, 8/5...前n项序列之和
- 微信小程序地址位置定位wx.getLocation 4G 与WIFI 不一致