将latlong设置为Spinner

时间:2014-02-12 07:46:06

标签: android google-maps spinner

在我的应用程序中,我必须从微调器中选择位置,并且选定的位置应该通过标记显示在地图上。我正在尝试获取地点的latlong并在地图中使用以显示该位置的标记。 我正在使用latlong的代码: -

public String getLatLong(double latitude, double longitude)
    {
         StringBuilder result = new StringBuilder();
            try {
                Geocoder geocoder = new Geocoder(this, Locale.getDefault());
                List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
                if (addresses.size() > 0) 
                {
                    Address address = addresses.get(0);
                    result.append(address.getLocality()).append("\n");
                    result.append(address.getCountryName());
                }
            } catch (IOException e)
            {
                Log.e("tag", e.getMessage());
            }
        return result.toString();
    }

但我的问题是我无法将此latlong设置为我的微调器并设置om map。

我的微调器代码: -

  spinner1 = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> array_state = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,state);
        array_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(array_state);
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View arg1,int pos, long arg3) {   
                 Toast.makeText(parent.getContext(), 
                "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {                
            }

        });

        spinner2 = (Spinner)findViewById(R.id.spinner2);
        ArrayAdapter<String> array_states = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,states);
        array_states.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(array_states);
        spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View arg1,
                    int pos, long arg3) {
                 Toast.makeText(parent.getContext(), 
                            "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                             Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        Button send = (Button)findViewById(R.id.button1);
        send.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                intent = new Intent(MainActivity.this,Map.class);
                startActivity(intent);
            }
        });
        getLatLong(0,0);//Method declare here
    }

请帮我这样做。 如何使用微调器设置此latlong方法

1 个答案:

答案 0 :(得分:0)

  

我无法将此latlong设置为我的微调器并设置om map。

因此,当您将数据传递给适配器时以及执行时:

spinner1.setAdapter(array_state);

或者

spinner2.setAdapter(array_states);

这意味着您已将数据传递给Spinner。

  

大问题是你没有区分听众   Spinner是为了什么?

所以你可以使用匿名方法:

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View arg1,
                    int pos, long arg3) {
            // TODO Auto-generated method stub
    Toast.makeText(parent.getContext(), 
    "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
    Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
                                 And
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View arg1,
                    int pos, long arg3) {
                // TODO Auto-generated method stub
                 Toast.makeText(parent.getContext(), 
                "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                 Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
              ******************* Edited ****************************

看到这个你会得到答案。

How to implement search functionality for Google Map Api V2 Android?