잡동사니

반응형

질문

Android 휴대 전화에서 GPS 데이터를 가져 오려고하는데이 코드에서는 Latitude를 가져 오려고합니다.
한 번만 데이터를 얻고 싶기 때문에 onLocationChanged메서드를 사용하지 않았습니다.
오류나 경고를받지 못했습니다.

package com.example.gpstryxyz;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements LocationListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 10, this);
        }

        Location loc1 = new Location("gps");
        double value1 = loc1.getLatitude();

        TextView tv = (TextView) findViewById(R.id.gpsTextView);
        tv.setText(String.valueOf(value1));
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

그래서 나는 내 휴대폰에 앱을 만들고, GPS를 켜고, 밖으로 나가서 앱을 시작하면 출력은 다음과 같다.

0.0

답변1

onLocationChangeFunction 사용

@Override
public void onLocationChanged(Location location) {
    LatLng CurrentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
}


답변2

제 질문에 답하고 싶습니다.
위의 코드에서 requestPermissions () 메서드를 사용하지 않았고 if 문에 잘못된 종류의 권한이 있습니다.
또한 requestLocationUpdates () 메서드와 onRequestPermissionsResult () 콜백 메서드가 어떻게 작동하는지 몰랐습니다. (Ctrl Q가 여기에 도움이 됨)

package com.example.gpstryxyz;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements LocationListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //app seemed to work only with gps, but does not update during runtime, quit and restart app to get new location
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            LocationManager locationManager;
            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            //registration (if this call succeeds, it calls the onLocationChanged() method)
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, this);
        } else {
            TextView tv = (TextView) findViewById(R.id.gpsTextView);
            tv.setText(R.string.noPermission);
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) throws SecurityException{
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // permission is granted
            LocationManager locationManager;
            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, this);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        //the Location object this method brings has the location data in it
        double value1 = location.getLatitude();
        double value2 = location.getLongitude();
        TextView tv = (TextView) findViewById(R.id.gpsTextView);
        tv.setText(getString(R.string.latlonString, String.valueOf(value1), String.valueOf(value2)));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(MainActivity.this, "gps is activated.....", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(MainActivity.this, "gps is NOT activated.....", Toast.LENGTH_LONG).show();
    }
}


 

 

 

 

출처 : https://stackoverflow.com/questions/60802030/android-studio-get-gps-data

반응형

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band