0

In my app I have to choose places from spinners and the selected place should be shown on the Map through marker .I am trying to get place's latlong and use in the map to show marker on that place . I am using code for 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();
    }

But My problem is that I can't set this latlong to my spinner and set om map.

My code of spinner:-

  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
    }

Please help me to do this . How do I set this latlong method with spinner

1 Answer 1

0

I can't set this latlong to my spinner and set om map.

So when you are passing your data to adapter and when you are doing :

spinner1.setAdapter(array_state);

Or

spinner2.setAdapter(array_states);

It means that you have already pass the data to Spinner.

Big Problem is that you haven't differenciate the listener for which Spinner is this for?

So you can make anonymous method like:

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 ****************************

See this You will get your answer.

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

4
  • thanks.I have corrected it.But this is not my problem.I want how to get my spinner area's latlong.through my code I can't guess how to do it
    – Barun
    Feb 12, 2014 at 10:00
  • Actually I dont get you what are you asking for?
    – Sagar Shah
    Feb 12, 2014 at 10:14
  • when I choose My places in spinner I want to show marker on map on that places
    – Barun
    Feb 12, 2014 at 10:19
  • When you select spinner item are you getting lat-long or City Name?
    – Sagar Shah
    Feb 12, 2014 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.