アンドロイド - ListView項目をクリックしたときにマップするために場所をピン留め?

StackOverflow https://stackoverflow.com/questions/4606557

質問

私は、タクシー会社のカップルが含まれているリストビューを持っています。タクシーの名前をユーザーがクリックすると、それはどちらかの本にそれらを求めてalertdialogボックスを表示すると、マップまたは取り消しを示しています。ユーザーがクリックは地図を表示すると何イムを達成しようとすると、それは既に設定されている文字列配列から場所をつかみ、その後、地図上を指しています。私はそれはまだのように私の現在の場所を見つけたいいけません。これは行うことは可能でしょうか?私はすでにチュートリアルからランダムな位置にセットへの本だけポイントでのMapViewを設定しています。

これは、両方のために私のコードです:

    ListTaxi Class : 

             class Taxi {
    private String taxiName;
    private String taxiAddress;
    private String taxiDist;

    public String getName() {
        return taxiName;
    }

    public void setName(String name) {
        taxiName = name;
    }

    public String getAddress() {
        return taxiAddress;
    }

    public void setAddress(String address) {
        taxiAddress = address;
    }
    public String getDist() {
        return taxiDist;
    }
    public void setDist(String dist){
        taxiDist = dist;
    }
    public Taxi(String name, String address, String dist) {
        taxiName = name;
        taxiAddress = address;
        taxiDist = dist;
    }
}

public class TaxiAdapter extends ArrayAdapter<Taxi> {
    private ArrayList<Taxi> items;
    private TaxiViewHolder taxiHolder;

    private class TaxiViewHolder {
        TextView name;
        TextView address; 
        TextView dist;
    }

    public TaxiAdapter(Context context, int tvResId, ArrayList<Taxi> items) {
        super(context, tvResId, items);
        this.items = items;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.taxi_list_item, null);
            taxiHolder = new TaxiViewHolder();
            taxiHolder.name = (TextView)v.findViewById(R.id.taxi_name);
            taxiHolder.address = (TextView)v.findViewById(R.id.taxi_address);
            taxiHolder.dist = (TextView)v.findViewById(R.id.taxi_dist);
            v.setTag(taxiHolder);
        } else taxiHolder = (TaxiViewHolder)v.getTag(); 

        Taxi taxi = items.get(pos);

        if (taxi != null) {
            taxiHolder.name.setText(taxi.getName());
            taxiHolder.address.setText(taxi.getAddress());
            taxiHolder.dist.setText(taxi.getDist());
        }

        return v;
    }
}

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

    final String[] taxiNames = getResources().getStringArray(R.array.taxi_name_array);
    final String[] taxiAddresses = getResources().getStringArray(R.array.taxi_address_array);
    final String[] taxiDist = getResources().getStringArray(R.array.taxi_array_dist);
    ArrayList<Taxi> taxiList = new ArrayList<Taxi>();

    for (int i = 0; i < taxiNames.length; i++) {
        taxiList.add(new Taxi(taxiNames[i], taxiAddresses[i], taxiDist[i]));

    }


    setListAdapter(new TaxiAdapter(this, R.layout.taxi_list_item, taxiList));  

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, final int position, long id)
        {       

            final int selectedPosition = position;
            AlertDialog.Builder adb=new AlertDialog.Builder(ListTaxi.this); 
             adb.setTitle("Taxi Booking");
             adb.setMessage("You Have Selected: "+taxiNames[selectedPosition]); 
             adb.setPositiveButton("Book", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     Intent intent = new Intent(ListTaxi.this, Booking.class);
                     intent.putExtra("booking",  taxiNames[selectedPosition]);
                     intent.putExtra("address",  taxiAddresses[selectedPosition]);
                     intent.putExtra("distance", taxiDist[selectedPosition]);
                     startActivity(intent);
                 }
             });
             adb.setNeutralButton("Show Map", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     Intent intent = new Intent(ListTaxi.this, Map.class);
                     startActivity(intent);
                 }
             });

             adb.setNegativeButton("Cancel", null); 
             adb.show();
         }
     });     
}

}

と私のマップクラスは次のとおりです。                 / **  *  * / パッケージcom.taxime;

    public class Map extends MapActivity{
LinearLayout linearLayout;
MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
ItemizedOverlay itemizedOverlay;


public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapviews);

        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);



        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        itemizedOverlay = new ItemizedOverlay(drawable);

        Intent intent = getIntent();
        if (intent != null) {
            if (Intent.ACTION_VIEW.equals(intent.getAction())) {
                if (intent.getData() != null) {
                    int selectedPosition = Integer.parseInt(intent.getData().getHost());
                    GeoPoint point = new GeoPoint(selectedPosition, selectedPosition);

                    OverlayItem overlayitem = new OverlayItem(point, "", "");
                    itemizedOverlay.addOverlay(overlayitem);
                    mapOverlays.add(itemizedOverlay);

                    //...
                }
            }
        }

また、私の文字列が来るから

    <string-array name="taxi_name_array">
    <item>Barrys Taxi</item>
    <item>Boom Taxi</item>

</string-array>

<string-array name="taxi_address_array">
<item>24 test address, Uxbridge, UB8 2JY</item>
<item>14 test 2 address, Uxbridge, UB91JD</item>
</string-array>
<string-array name = "taxi_array_dist">
<item>0.4 miles</item>
<item>0.8 miles</item>
</string-array>
<string-array name ="location">
<item>(19240000,-99120000)</item>
</string-array>
誰もが任意のアイデアを持っている場合は、

それは素晴らしいだろう。

おかげで再びみんな

役に立ちましたか?

解決

あなたはどうすることができますすると、マップの活動にクリックされたタクシーの項目の位置を渡す意思のデータフィールドを使用しています。これを行うには、「地図」ボタンのonClickハンドラ内、あなたは4つの引数を取りIntentコンストラクタを使用します:

adb.setNeutralButton("Show Map", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("taxi:" + selectedPosition), ListTaxi.this, Map.class);
        startActivity(intent);
    }
});

すると、マップのアクティビティの内側に、お電話それを開始しgetIntentを取得するIntent の方法。意図取り出したデータフィールドに getData に呼び出しますます。

は、すべて一緒にそれを置く、あなたはマップの活動のonCreateオーバーライドして、次のようなものを持っています

Intent intent = getIntent();
if (intent != null) {
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        if (intent.getData() != null) {
            int selectedPosition = Integer.parseInt(intent.getData().getHost());
            String[] taxiLocations = getResources().getStringArray(R.array.location);
            String location = taxiLocations[selectedPosition];
            //...
        }
    }
}

他のヒント

あなたがアドレスを持っている必要がある緯度/経度にマップを設定するには?意味しますか

:おそらくこのような何か
    Geocoder gc = new Geocoder(this.getApplicationContext()) ;
    List<Address> addresses = null ;

    try {
       addresses = gc.getFromLocationName("123 Main St, Peoria IL 61601", 10) ;
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }

    Address a = addresses.get(0) ;
    double lat = a.getLatitude() ;
    double lon = a.getLongitude() ;

    MapController mc = mapView.getController();

    p = new GeoPoint(
        (int) (lat * 1E6), 
        (int) (lon * 1E6));

    mc.animateTo(p);
    mc.setZoom(18);
    mapView.invalidate();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top