web-dev-qa-db-ja.com

Androidで緯度と経度で都市名を取得する方法は?

現在の場所で都市名にしたいのですが、緯度と経度があります。ボタンクリックを使用すると、緯度と経度の値が2倍になります。下記の私のコード。私を助けてください。

ありがとう!!

Button btnShowLocation;

    GPSTracker gps;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);

        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {       
                // create class object
                gps = new GPSTracker(AndroidGPSTrackingActivity.this);

                // check if GPS enabled     
                if(gps.canGetLocation()){

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                    // \n is for new line
                    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
                }else{

                    gps.showSettingsAlert();
                }

            }
        });

私は以下のコードを編集しましたが、都市名を取得できません助けてください!!!

Button btnShowLocation;

    GPSTracker gps;
    String CityName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);

        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // create class object
                gps = new GPSTracker(AndroidGPSTrackingActivity.this);

                // check if GPS enabled
                if (gps.canGetLocation()) {

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                    Geocoder geocoder = new Geocoder(
                            AndroidGPSTrackingActivity.this, Locale
                                    .getDefault());
                    List<Address> addresses;
                    try {
                        Log.v("log_tag", "latitude" + latitude);
                        Log.v("log_tag", "longitude" + longitude);
                        addresses = geocoder.getFromLocation(latitude,
                                longitude, 1);
                        Log.v("log_tag", "addresses+)_+++" + addresses);
                        CityName = addresses.get(0).getAddressLine(0);
                        Log.v("log_tag", "CityName" + CityName);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // \n is for new line
                    Toast.makeText(
                            getApplicationContext(),
                            "Your Location is - \nLat: " + latitude
                                    + "\nLong: " + longitude, Toast.LENGTH_LONG)
                            .show();
                } else {

                    gps.showSettingsAlert();
                }

            }
        });

しかし、以下でエラーが発生します::::

03-11 17:01:46.465: W/System.err(27587): Java.io.IOException: Service not Available
03-11 17:01:46.465: W/System.err(27587):    at Android.location.Geocoder.getFromLocation(Geocoder.Java:136)
03-11 17:01:46.465: W/System.err(27587):    at com.example.gpstracking.AndroidGPSTrackingActivity$1.onClick(AndroidGPSTrackingActivity.Java:81)
03-11 17:01:46.465: W/System.err(27587):    at Android.view.View.performClick(View.Java:4191)
03-11 17:01:46.475: W/System.err(27587):    at Android.view.View$PerformClick.run(View.Java:17229)
03-11 17:01:46.475: W/System.err(27587):    at Android.os.Handler.handleCallback(Handler.Java:615)
03-11 17:01:46.475: W/System.err(27587):    at Android.os.Handler.dispatchMessage(Handler.Java:92)
03-11 17:01:46.475: W/System.err(27587):    at Android.os.Looper.loop(Looper.Java:137)
03-11 17:01:46.475: W/System.err(27587):    at Android.app.ActivityThread.main(ActivityThread.Java:4960)
03-11 17:01:46.475: W/System.err(27587):    at Java.lang.reflect.Method.invokeNative(Native Method)
03-11 17:01:46.475: W/System.err(27587):    at Java.lang.reflect.Method.invoke(Method.Java:511)
03-11 17:01:46.475: W/System.err(27587):    at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:1038)
03-11 17:01:46.475: W/System.err(27587):    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:805)
03-11 17:01:46.475: W/System.err(27587):    at dalvik.system.NativeStart.main(Native Method)
03-11 17:02:21.335: W/IInputConnectionWrapper(27587): getSelectedText on inactive InputConnection
03-11 17:02:21.335: W/IInputConnectionWrapper(27587): setComposingText on inactive InputConnection
03-11 17:02:21.425: W/IInputConnectionWrapper(27587): getExtractedText on inactive InputConnection
30
crickpatel0024

これを使って:

 Geocoder geocoder = new Geocoder(this, Locale.getDefault());
 List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
 String cityName = addresses.get(0).getAddressLine(0);
 String stateName = addresses.get(0).getAddressLine(1);
 String countryName = addresses.get(0).getAddressLine(2);

詳細なGoogleマップの例については、以下のリンクを確認してください: http://www.demoadda.com/demo/Android/load-googlemap_107

そして、バックグラウンドでの場所の更新の場合: http://www.demoadda.com/demo/Android/download-Android-background-location-update-service-demo_21

65
Kishan

OnClickでこれを追加します。

Geocoder gcd = new Geocoder(AndroidGPSTrackingActivity.this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) 
    System.out.println(addresses.get(0).getLocality());
23
Mayank Saini
gps = new LocationAddress(getActivity());
if (gps.canGetLocation()) {
  double latitude = gps.getLatitude();
  double longitude = gps.getLongitude();
  Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
  //List<Address> addresses =geocoder.getFromLocation(latitude, longitude, 1);

  try {
    List < Address > addresses = geocoder.getFromLocation(latitude, longitude, 1);
    String address = addresses.get(0).getSubLocality();
    String cityName = addresses.get(0).getLocality();
    String stateName = addresses.get(0).getAdminArea();
    txt_paddress.setText(address);
    txt_city.setText(cityName);
    txt_state.setText(stateName);

  } catch (IOException e) {
    e.printStackTrace();
  }
} else {
  gps.showSettingsAlert();
}

ジオコーダーを使用します。緯度と経度を渡し、都市名と住所を取得します。

try {

    Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
    List < Address > addresses = geo.getFromLocation(latitude, longitude, 1);
    if (addresses.isEmpty()) {
        addres.setText("Waiting for Location");
    } else {
        if (addresses.size() > 0) {
            addres.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() + ", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
            //Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
        }
    }
} catch (Exception e) {
    e.printStackTrace(); // getFromLocation() may sometimes fail
}
2
Shadow

私の国(ベトナム)の場合、getLocalityはnullを返し、addresses.get(0).getAddressLine(0)district not cityを返します

だから私はこのようにします

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);

int maxAddressLine = addresses.get(0).getMaxAddressLineIndex();

String countryName = addresses.get(0).getAddressLine(maxAddressLine);
String stateName = addresses.get(0).getAddressLine(maxAddressLine-1);
String cityName = addresses.get(0).getAddressLine(maxAddressLine-2);
1
Phan Van Linh