GPS 사용!!

2014. 7. 7. 22:20Programming/Android

반응형

//GPS Setting    onCreate 영역!!

        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        Criteria c = new Criteria();

        provider = lm.getBestProvider(c, true);

        if(provider==null || !lm.isProviderEnabled(provider)){

        List<String> list = lm.getAllProviders();

        for(int i=0; i < list.size(); i++){

        String temp = list.get(i);

        if(lm.isProviderEnabled(temp)){

        provider = temp;

        break;

        }

        }

        } // GPS search end

        

        Location location = lm.getLastKnownLocation(provider);

        if(location ==null){

        Toast.makeText(this, "Not exist provider", Toast.LENGTH_SHORT).show();

        } else{

        onLocationChanged(location);

        }


//onResume 영역!!

lm.requestLocationUpdates(provider, 500, 1, this);


//추가 메소드 들~

public String getAddress(double lat, double lng){

    String address = null;

    

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());

    

    List<Address> list = null;

    

    try {

list = geocoder.getFromLocation(lat, lng, 1);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    

    if(list == null){

    Log.e("getAddress","Data get fail");

    }

    

    if(list.size()>0){

    Address addr = list.get(0);

    address = addr.getCountryName()+" "

    +addr.getLocality()+" "

    +addr.getThoroughfare()+"+"

    +addr.getFeatureName();

    }

    

    return address;

}


@Override   추상 메소드

public void onLocationChanged(Location location) {

// TODO Auto-generated method stub

lat = location.getLatitude();

lng = location.getLongitude();

Log.d("Location",getAddress(lat,lng));

}

반응형

'Programming > Android' 카테고리의 다른 글

Button animation 만들기!  (0) 2014.07.20
Activity 화면 꺼지지 않게 유지하기!  (0) 2014.07.11
GyroSensor 사용하기  (0) 2014.07.11
Title bar 제거 하기!  (0) 2014.07.11
[Android] 다음 지도 API 사용하기  (0) 2014.06.29