GpsProvider与NetWorkProvider

package com.location;
//    <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" />
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	Button showGeroInfoBtn;
	TextView showGeroInfoText;
	Button showGcsInfoBtn;
	TextView showGcsInfoText;
    private double latGps;
    private double lonGps;
    private double latNet;
    private double lonNet;   
    Location locationGps;
    Location locationNet;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//showGeroInfoBtn=(Button) findViewById(R.id.showGeroInfoBtn);
		showGeroInfoText=(TextView) findViewById(R.id.showGcsInfoText);
		showGcsInfoBtn=(Button) findViewById(R.id.showGcsInfoBtn);
		showGcsInfoText=(TextView) findViewById(R.id.showGcsInfoText);
		showGcsInfoBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
			        locationGps=locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
				locationNet=locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
                               //gps与net同时声明,但系统只会选择一个使用,太不科学了。
				LocationListener locationListenerGps=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
						
					}
					
					@Override
					public void onProviderDisabled(String provider) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onLocationChanged(Location location) {
						// TODO Auto-generated method stub
						locationGps=location;
                                                   //空代码未删。
						latGps=location.getLatitude();
						lonGps=location.getLongitude();
						Log.e("GGGGGGGGG", latGps+"_!!!___!!!_"+lonGps);
					}
				};
				LocationListener locationListenerNet=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
						
					}
					
					@Override
					public void onProviderDisabled(String provider) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onLocationChanged(Location location) {
						// TODO Auto-generated method stub
						locationNet=location;
						latNet=location.getLatitude();
						lonNet=location.getLongitude();
						Log.e("NNNNNNNNNN", latNet+"_!!!___!!!_"+lonNet);						
					}
				};
				locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 1000, 0, locationListenerGps);
				//locationGps=locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
				locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 1000, 0, locationListenerNet);
				//locationNet=locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
				showGeroInfoText.setText("经度:"+latGps+";"+"纬度:"+lonGps+"Gps");
				showGcsInfoText.setText("经度:"+latNet+";"+"纬度:"+lonNet+"Net");
				}});
			}
	//屋内手机实测,network得到经纬度,gps没有,看来系统自动选的net。不知道系统是随即选的还是根据信号强弱选的。如果是前者,需要加if语句自己选择,如果是后者,那直接全写交给系统就行。此外,开启后须等待十来秒之后才会更新。
/*
    if(locationGps==null){

    showGcsInfoText.setText("经度:"+latNet+";"+"纬度:"+lonNet+"Net");

    }else if(locationNet==null){
 
    showGcsInfoText.setText("经度:"+latGps+";"+"纬度:"+lonGps+"Gps");
 
    }
*/
//试过用asynctask处理,但更新Ui时报错,不懂为什么。个人猜测为execute()方法只能执行一次,后来location更新再执行出错。试试handler,如果同样不行,那我的猜测应该是错的。
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

api参考 https://developers.google.com/maps/documentation/geocoding/?hl=zh-CN&csw=1 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android Studio中进行GPS定位,可以使用LocationManager和LocationProvider这两个类来实现。 首先,需要获取LocationManager的实例,可以通过以下代码实现: LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 然后,可以通过LocationManager的getProvider方法来获取不同的LocationProvider。例如,通过GPS_PROVIDER获取GPS定位提供者: LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER); 另外,你也可以使用NETWORK_PROVIDER来获取网络定位提供者: LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER); 在具体应用中,你可以根据需求选择使用GPS定位还是网络定位来获取当前位置和经纬度坐标。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Android Studio GPS三种定位](https://blog.csdn.net/wangz666/article/details/92713235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Android获取GPS定位示例程序](https://download.csdn.net/download/m1m2m3mmm/13061489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值