0

パッケージ net.learn2develop.PopularAttractions;

import java.io.IOException; java.util.List をインポートします。java.util.Locale をインポートします。

com.google.android.maps.GeoPoint をインポートします。com.google.android.maps.MapActivity をインポートします。com.google.android.maps.MapController をインポートします。com.google.android.maps.MapView をインポートします。

android.location.Address をインポートします。android.location.Geocoder をインポートします。android.os.Bundle をインポートします。android.util.Log をインポートします。android.view.KeyEvent をインポートします。android.view.View をインポートします。

android.widget.AdapterView をインポートします。android.widget.ArrayAdapter をインポートします。android.widget.Spinner をインポートします。android.widget.Toast をインポートします。android.widget.AdapterView.OnItemSelectedListener; をインポートします。

public class PopularAttractions は MapActivity を拡張します {

private String[ ][ ] locations = {
        {"Chinatown Heritage Center","1.2836,103.84425"},
        {"Escape Theme Park","1.38104,103.936928"},
        {"G-Max Reverse Bungy","1.2906,103.845322"},
        {"Jurong BirdPark","1.32005,103.707153"},
        {"NEWater Visitor Center","1.33105,103.955311"},
        {"Red Dot Design Museum","1.277762,103.846225"},
        {"Singapore Botanic Garden","1.31471,103.815689"},
        {"Singapore Science Center","1.3249,103.740578"},
        {"Singapore Zoological Garden","1.40502,103.793449"},
        {"Snow City","1.32823,103.74263"},
        {"Sungei Buloh Wetland Reserver","1.445144,103.729595"},
        {"Super Ice World","1.300422,103.875348"},
    };

 Spinner spinnerView;
 MapView mapView;
 MapController mc;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    spinnerView = (Spinner) this.findViewById(R.id.spinner1);
    mapView = (MapView) findViewById(R.id.mapview1);
    mc = mapView.getController();

    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item);

    for(int i = 0; i < locations.length; i++)
        adapter.add(locations[i][0]);


    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerView.setAdapter(adapter);
    spinnerView.setOnItemSelectedListener(selectListener);

    gotoSelected();
}



private OnItemSelectedListener selectListener = new OnItemSelectedListener() {

    public void onItemSelected(
            AdapterView<?>parent, View v, int position, long id)
    {
        gotoSelected();
    }

    public void onNothingSelected(AdapterView<?> arg0) {}

};


public void gotoSelected()
{

    int index = spinnerView.getSelectedItemPosition();
    String[] coordinates = locations[index][1].split(",");
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
         Address ad = addresses.get(0); 
        String buff = new String();
    for(int i = 0; i <= ad.getMaxAddressLineIndex(); i++ ) { 
             buff += ad.getAddressLine(i);
     Toast.makeText(getBaseContext(), buff, Toast.LENGTH_SHORT).show();
  GeoPoint location = new GeoPoint (
        (int)(lat * 1E6),
        (int)(lng * 1E6));

            mc.animateTo(location);
            mc.setZoom(16);

      mapView.setStreetView(true);


     }
     for (Address a : addresses) { 
         Log.v("TAG", a.toString()); 
     } 


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

}

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    MapController mc = mapView.getController(); 
    switch (keyCode) 
    {
        case KeyEvent.KEYCODE_2:
            mc.zoomIn();
            break;
        case KeyEvent.KEYCODE_1:
            mc.zoomOut();
            break;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}
4

1 に答える 1

0

Geocoder を試すことができます: https://developer.android.com/reference/android/location/Geocoder.html

for(int i = 0; i < locations.length; i++) {

        List<Address> list;
        Geocoder geo = new Geocoder(this); 
        try {
                String[] latlong = locations[i][1].split(",");
                list = geo.getFromLocation(Double.valueOf(latlong[0]), Double.valueOf(latlong[1]), 1); // the 1st result should be the best one
                Address ad = list.get(0); // Position 0 is the most specific to the coordinates.
                String buff = new String(); 
                for(int i = 0; i <= ad.getMaxAddressLineIndex(); i++ ) {
                     buff += ad.getAddressLine(i); // for a good addres(with street name u should get getMaxAddressLineIndex() = 2
                     // HERE you would want to do something with buff to show or to store it

                 }


                for (Address a : list) {
                    Log.v("TAG", a.toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

通常、リストの最初のものが興味のあるものになります。各 Address について、getAddressLine(int pos) で住所を取得します。通常、位置 0 に通りの名前、位置 1 に郵便番号 + 都市、次の位置に国を取得します。ただし、これを確認するのに十分なテストを行っていません。

アドレスを取得するには、次のようなものを使用できます。

Address ad = list.get(0); // Position 0 is the most specific to the coordinates.
String buff = new String(); 
for(int i = 0; i <= ad.getMaxAddressLineIndex(); i++ ) {
    buff += ad.getAddressLine(i); // for a good addres(with street name u should get getMaxAddressLineIndex() = 2
}
于 2010-04-28T10:04:40.893 に答える