Android之获取网络位置的经纬度

来源:互联网 发布:百度贴吧衰落知乎 编辑:程序博客网 时间:2024/06/11 07:34

上一篇已经说了一下基于位置的服务~

这一篇想说一下基于网络的位置提供者获取到经纬度

直接贴代码吧,我是获取了聚合数据的位置,然后在获取到经纬度的,

其实也可以获取到自己位置的经纬度,那就是基于GPS的位置提供者了~下一篇说一下

public class MainActivity extends Activity {private TextView address,business,tv;private LocationManager mLocationManager;private double Latitude,Longitude;private String addressText,businessText;@Overrideprotected 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() {@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}//位置服务可用@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stubupdateLocation(mLocationManager.getLastKnownLocation(provider));}@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}//位置改变@Overridepublic void onLocationChanged(Location location) {// TODO Auto-generated method stubupdateLocation(location);}});System.out.println("11111--------------------------");Http();System.out.println("222222---------------------");}private void Http() {// TODO Auto-generated method stubHttpUtils 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>(){@Overridepublic void onFailure(HttpException arg0, String arg1) {// TODO Auto-generated method stub}@Overridepublic void onSuccess(ResponseInfo<String> arg0) {// TODO Auto-generated method stubSystem.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 blocke.printStackTrace();}}});}private void updateLocation(Location mlocation) {// TODO Auto-generated method stubif(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




1 0