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

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

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

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

其实也可以获取到自己位置的经纬度,那就是基于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




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值