필요한 permission

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


MainActivity.java

package com.rosa.test.location;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    private LocationManager locationMgr;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        locationMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        
        //Criteria 클래스를 이용하여 요구조건을 명시하여, 가장 적합한 기술을 결정
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);
        
        //true=현재 이용가능한 공급자 제한 (return String)
        //false (return List<String>)
        String bestProvider = locationMgr.getBestProvider(criteria, true);
        
        //현재 위치        
        Location location = locationMgr.getLastKnownLocation(bestProvider);
        
        if(location != null){
            Toast.makeText(this, "위도:"+location.getLatitude()+"\n"+"경도:"+location.getLongitude(), 3000).show();
        }
    }
}


결과화면






저작자 표시 비영리 변경 금지
신고
Posted by DoDo4989

댓글을 달아 주세요