안녕하세요 현재 기상앱을 만들고 있는 중입니다.
현재 플러그인에 있는 gps 에뮬레이터를 이용해서 gps값을 받아오는 것을 목표로 하는데
뭐가 문제인지 위도 값을 0으로 받아 오는거 같습니다. 그래서 액티비티에 아무것도 출력이 되지 않구요.
매니페스트도 다 넣었는데 왜 안되는지 알수 있을까요 ㅠㅠ?
LocationManager locationManager;
private Context mContext;
boolean isGPSEnabled = false;
//현재 gps 사용유무
boolean isNetWorkEnabled = false;
//네트워크 사용 유무
boolean isGetLocation = false;
//gps 상태값
Location location;
double lat;
double lon;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//gps 정보 가져오기
isNetWorkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
//현재 네트워크 상태 값 알아오기
if (!isGPSEnabled && !isNetWorkEnabled) {
Toast.makeText(getApplicationContext()," gps와 네트워크 사용이 불가능합니다." ,Toast.LENGTH_SHORT).show();
} else {
this.isGetLocation = true;
try {
if (isNetWorkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
//네트워크 정보로부터 위치값 가져오기
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
//위도 경도 저장
lat = location.getLatitude();
lon = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lon = location.getLongitude();
}
}
}
}
} catch (SecurityException e) {
Log.d("error","오류",e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(),"onLocationChanged",Toast.LENGTH_SHORT).show();
getWeatherData(location.getLatitude(),location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
private void getWeatherData (double lat,double lon){
String url = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat +
"&lon=" + lon +"&units=metric&appid=081bed6a85d281e8a3b8f5d2903293f9";
ReceiveWeatherTask receiveWeatherTask = new ReceiveWeatherTask();
receiveWeatherTask.execute(url);
}
private class ReceiveWeatherTask extends AsyncTask<String, Void, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... params) {
try{
HttpURLConnection conn = (HttpURLConnection) new URL(params[0]).openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(is);
BufferedReader in = new BufferedReader(reader);
String readed;
while ((readed = in.readLine()) != null) {
JSONObject jsonObject = new JSONObject(readed);
return jsonObject;
}
}else {
return null;
}
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONObject result) {
Log.i("error",result.toString());
if(result != null) {
String iconName="";
String nowTemp="";
String humidity="";
String main ="";
String description = "";
String city="";
String update="";
try{
iconName = result.getJSONArray("weather").getJSONObject(0).getString("icon");
nowTemp=result.getJSONObject("main").getString("temp");
humidity=result.getJSONObject("main").getString("humidity");
main=result.getJSONArray("weather").getJSONObject(0).getString("main");
description=result.getJSONArray("weather").getJSONObject(0).getString("description");
city=result.getString("name").toUpperCase(Locale.US) + "," +result.getJSONObject("sys")
.getString("country");
DateFormat df = DateFormat.getDateInstance();
update =df.format(new Date(result.getLong("dt")*1000));
icon.setText(iconName);
temp.setText(nowTemp);
Humidity.setText(humidity);
Cityfield.setText(city);
Update.setText(update);
} catch (JSONException e) {
Log.d("error","오류",e);
}
}
}
}
}