上一篇已经说了一下基于位置的服务~
这一篇想说一下基于网络的位置提供者获取到经纬度
直接贴代码吧,我是获取了聚合数据的位置,然后在获取到经纬度的,
其实也可以获取到自己位置的经纬度,那就是基于GPS的位置提供者了~下一篇说一下
public class MainActivity extends Activity {
private TextView address,business,tv;
private LocationManager mLocationManager;
private double Latitude,Longitude;
private String addressText,businessText;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);
tv=(TextView) findViewById(R.id.tv);
address=(TextView) findViewById(R.id.address);
business=(TextView) findViewById(R.id.business);
mLocationManager=(LocationManager) getSystemService(LOCATION_SERVICE);
Location mlocation=mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
updateLocation(mlocation);
//每隔5秒 2000米的距离
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 2000, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
//位置服务可用
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
updateLocation(mLocationManager.getLastKnownLocation(provider));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
//位置改变
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateLocation(location);
}
});
System.out.println("11111--------------------------");
Http();
System.out.println("222222---------------------");
}
private void Http() {
// TODO Auto-generated method stub
HttpUtils httpUtils=new HttpUtils(30000);
String url="http://apis.juhe.cn/geo/?key=66241956000a0e18bd57cbb1378d3382&lat=27.327578&lng=119.9772857&type=1";
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>(){
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
System.out.println(arg0.result);
try {
JSONObject jsonObject=new JSONObject(arg0.result);
JSONObject resultObject=jsonObject.getJSONObject("result");
addressText=resultObject.getString("address");
businessText=resultObject.getString("business");
address.setText("address:"+addressText);
business.setText("business:"+businessText);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private void updateLocation(Location mlocation) {
// TODO Auto-generated method stub
if(mlocation!=null){
Longitude=mlocation.getLongitude();
Latitude=mlocation.getLatitude();
tv.setText("Longitude:"+Longitude+"\n"+"Latitude:"+Latitude);
}else{
tv.setText("正在获取位置信息");
}
}
}
布局文件就不上了~
加上权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
要源码的:http://download.csdn.net/detail/qq_33642117/9584928