Notice
Recent Posts
Recent Comments
Link
관리 메뉴

설.현.아빠

현재 Location 정보 얻기 본문

안드로이드/Map

현재 Location 정보 얻기

설.현.아빠 2011. 2. 11. 10:50






필요한 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();
        }
    }
}


결과화면






Comments