사용자에게 GPS를 켤 수있는 가능성을 제공하는 Android 프로젝트를 진행 중입니다. 응용 프로그램을 시작하려면 그의 위치가 요청됩니다.
활성화 후 LocationManager의 getLastKnownLocation을 사용하지만이 메서드는 주어진 공급자로부터 얻은 마지막으로 알려진 위치 수정의 데이터를 나타내는 Location을 반환합니다. 이 문서는 공급자를 시작하지 않고도이 작업을 수행 할 수 있다고 설명합니다. 예를 들어 장치가 꺼져 있고 다른 위치로 이동 한 경우이 위치가 최신이 아닐 수 있습니다.
그러나 사용자는 현지화 서비스를 활성화합니다. 따라서 GPS를 켜는 것만으로는 Google지도에서 기기 상단에 아이콘을 표시하고 GPS 좌표를 검색하여 GPS를 실행하는 데 충분하지 않습니다. 내 응용 프로그램이 사용자에게 GPS를 활성화 할 수있는 가능성을 제공 할 때이 아이콘이 나타나지 않습니다 (이상하게 보임).
내가 이미 읽은 Theards는 최고의 공급자를 선택한 후 getLastKnownLocation 메서드를 사용합니다. 질문은 GPS 서비스를 켠 후 놓친 것이 있습니까? GPS가 활성화되면 GPS가 자동으로 실행 되었습니까?
지도에 파란색 점 아이콘을 표시하려면이 선을 사용하세요.
map_fragment.setMyLocationEnabled(true);
이 코드를 사용하여 위치를 얻으려는 위치를 가져옵니다.
GPSTracker gps = new GPSTracker(this);
Double latiDouble = gps.getLatitude();
Double loDouble = gps.getLongitude();
GPSTracker 클래스를 만들고이 코드를 추가합니다.
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
boolean isGPSEnabled=false;
boolean isNetworkEnabled=false;
boolean canGetLocation=false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10; //10 meters
private static final long MIN_TIME_BW_UPDATES=1000*60*1; //1 minute
private LocationManager locationManager;
public GPSTracker(Context context)
{
this.mContext=context;
getLocation();
}
public Location getLocation()
{
locationManager=(LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled)
{
//showSettingAlert();
}
else
{
this.canGetLocation=true;
if(isGPSEnabled)
{
if(location==null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
}
if(locationManager != null)
{
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
latitude=location.getLatitude();
longitude=location.getLongitude();
}
}
}
if(isNetworkEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
if(locationManager!=null)
{
location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null)
{
latitude=location.getLatitude();
longitude=location.getLongitude();
}
}
}
}
return location;
}
public void stopUsingGPS()
{
if(locationManager!=null)
{
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude()
{
if(location != null)
{
latitude=location.getLatitude();
}
return latitude;
}
public double getLongitude()
{
if(location != null)
{
longitude=location.getLongitude();
}
return longitude;
}
public boolean canGetLocation(){
return this.canGetLocation;
}
public void showSettingAlert()
{
AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS Settings");
alertDialog.setMessage("GPS is not enabled .Do you want to go to settings menu ?");
alertDialog.setPositiveButton("Settings",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(i);
}
});
alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다