gps구현중입니다.
사진에 보시다시피 위도와 경도는 잘 잡아오는것 같은데요. 이
정보를 이용하여 주소를 찍고싶은데요; 텍스트뷰에 아무것도 나오질 않네요.
package com.example.gpsfinalksj;
+import java.io.IOException
public class MainActivity extends Activity{
//LocationManager의 선언이 반드시 전역변수로 있어야 한다.
private LocationManager locationmanager;
private Criteria criteria;
private TextView mylocationText, adr;
private Context context;
//private double latitude, longitude;
private Location location;
private String mAddressStr, addressString, Str, StrLocal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String context = Context.LOCATION_SERVICE;
locationmanager = (LocationManager)getSystemService(context);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationmanager.getBestProvider(criteria, true);
Location location = locationmanager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationmanager.requestLocationUpdates(provider, 10000, 100, new MyLocationListener());
//locationmanager.requestLocationUpdates(provider, 10000, 100, this);
if(provider==null) {
Toast.makeText(getBaseContext(), "No GPS Provider", Toast.LENGTH_SHORT).show();
provider=LocationManager.NETWORK_PROVIDER;
location=locationmanager.getLastKnownLocation(provider);
getAddress();
}
location=locationmanager.getLastKnownLocation(provider);
if(location==null) {
try{
Thread.sleep(10000);
}catch(InterruptedException e) {
e.printStackTrace();
}
location = locationmanager.getLastKnownLocation(provider);
}
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
private void updateWithNewLocation(Location location) {
String latlng;
mylocationText = (TextView)findViewById(R.id.myLocationText);
if(location!=null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
latlng = "위도 : "+lat+"\n경도: "+lng;
Log.i("TAG", latlng);
}
else
{
latlng="위치를 찾을수 없습니다.";
}
mylocationText.setText("현재위치 : \n"+latlng);
}
public void getAddress() {
Geocoder geoCoder = new Geocoder(this);
double lat = location.getLatitude();
double lng = location.getLongitude();
StringBuffer juso = new StringBuffer();
try {
List<Address> addresses;
addresses = geoCoder.getFromLocation(lat, lng, 1);
//StringBuilder juso = new StringBuilder();
if(addresses != null && addresses.size()>0){
for(Address addr:addresses) {
int index = addr.getMaxAddressLineIndex();
//juso.append(index).append("********\n");
for(int i=0; i<=index; i++) {
//juso.append(addr.getAddressLine(i));
//juso.append(" ");
juso.append(addr.getAddressLine(i)).append("<<\n\n");
}
juso.append("===========\n");
}
Address mAddress = addresses.get(0);
juso.append(mAddress.getCountryName()).append("\n");
juso.append(mAddress.getPostalCode()).append("\n");
juso.append(mAddress.getLocality()).append("\n");
juso.append(mAddress.getThoroughfare()).append("\n");
juso.append(mAddress.getFeatureName()).append("\n");
String Area = mAddress.getCountryName();
mAddressStr = mAddress.getCountryName()+" "
+mAddress.getPostalCode()+" "
+mAddress.getLocality()+" "
+mAddress.getThoroughfare()+" "
+mAddress.getFeatureName();
Toast.makeText(getBaseContext(), mAddressStr, Toast.LENGTH_SHORT).show();
addressString = juso.toString();
adr.setText(addressString);
//juso.append(addressString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
==================================================================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="@+id/myLocationText"
android:layout_width="fill_parent"
android:layout_height="148dp"
android:layout_gravity="center"
android:textColor="#ffffff"
android:textSize="25sp" />
<TextView
android:id="@+id/adr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.44"
android:text="TextView" />
</LinearLayout>
======================================================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpsfinalksj"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
....
</manifest>