안드로이드 개발 질문/답변
(글 수 45,052)
안녕하십니까!!
앱 개발도중 GPS 에서 현제 위치를 들고오지 못하고 null을 리턴 시켜 버립니다
Location location = locationManager.getLastKnownLocation(provider);
여기서 location 이 null 로 리턴 되는 상태입니다
myeclipse 를 쓰구 애뮬은 Google Api 2.0 level 5 로 돌리고 있습니다
소스 첨부해 드리겠습니다!!
도데체 왜 그런가요 흑흑
앱 개발도중 GPS 에서 현제 위치를 들고오지 못하고 null을 리턴 시켜 버립니다
Location location = locationManager.getLastKnownLocation(provider);
여기서 location 이 null 로 리턴 되는 상태입니다
myeclipse 를 쓰구 애뮬은 Google Api 2.0 level 5 로 돌리고 있습니다
소스 첨부해 드리겠습니다!!
import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView;
public class Star_Music extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; String context = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(context); String provider = LocationManager.GPS_PROVIDER; Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); } private void updateWithNewLocation(Location location){ String latLongString; TextView myLocationText; myLocationText = (TextView)findViewById(R.id.myLocationText); if(location != null){ double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "위도:"+lat+"\n경도:"+lng; }else{ latLongString = "위치를 찾을수 없음"; } myLocationText.setText("당신의 현제 위치는:\n"+latLongString); } } <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.star.music" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Star_Music" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="com.google.android.maps"/> </application> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myLocationText" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
도데체 왜 그런가요 흑흑
2010.03.04 15:24:35
저도 에뮬로 할때 그런 현상이 있었는데
ddms 실행시키신후에 가상 GPS 좌표를 지정해보세요~
Emulator Control 탭에 스크롤 내리시면 Location Controls 라고 있습니다~
2010.04.11 11:40:41
getLastKnownLocation() 함수는 가장 최근에 획득한 GPS 정보를 가져옵니다.
만약 가장 최근에 얻어 온 정보가 없다면, 당연이 이 함수는 null 을 return 하게 됩니다.
추천을 드리고 싶은것은
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListener);
와 같이 request 을 하시고, mLocationListener 리스너에서, onLocationChanged() 함수가 호출되었을 때 Location 정보를 참조하시는게 가장 정확합니다.
GPS 의 가장 많은 예제 중에서 getLastKnownLocation() 을 사용하는데 이건 어찌 보면 Single shot 개념이고 Tracking 으로 원하시면 위의 예제처럼 하시는게 가장 쉽습니다.
만약 가장 최근에 얻어 온 정보가 없다면, 당연이 이 함수는 null 을 return 하게 됩니다.
추천을 드리고 싶은것은
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListener);
와 같이 request 을 하시고, mLocationListener 리스너에서, onLocationChanged() 함수가 호출되었을 때 Location 정보를 참조하시는게 가장 정확합니다.
GPS 의 가장 많은 예제 중에서 getLastKnownLocation() 을 사용하는데 이건 어찌 보면 Single shot 개념이고 Tracking 으로 원하시면 위의 예제처럼 하시는게 가장 쉽습니다.