-1

i want to build a app which shows me user location on google map...but it shows me no address is found ..even when i tried to give fixed value ...

     if(location!=null && !location.equals("")){
                    googleMap.clear();
                    new GeocoderTask(MainActivityMap.this).execute(location);

                }

My Geocoder Asynctask Activity

        private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
            private Context mainContxt;
            Geocoder geocoder;
            public GeocoderTask(Context con){
            mainContxt=con;

            } 

            @Override
            protected List<Address> doInBackground(String... locationName) {
                 Geocoder geocoder = new Geocoder(mainContxt);
                    List<Address> addresses = null;

                    try {
                        addresses = geocoder.getFromLocationName(locationName[0], 3);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return addresses;
            }


            @Override
            protected void onPostExecute(List<Address> addresses) { 


        if(addresses==null || addresses.size()==0){
      Toast.makeText(getBaseContext(), "No Location found.Please check       

      address", Toast.LENGTH_SHORT).show();
      return; // add this
       }
      else{

               for(int i=0;i<addresses.size();i++){             

                    Address address = (Address) addresses.get(i);
                    latLng = new LatLng(address.getLatitude(), address.getLongitude());

                    String addressText = String.format("%s, %s",
                            address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                            address.getCountryName());

                    markerOptions = new MarkerOptions();
                    markerOptions.position(latLng);
                    markerOptions.title(addressText);
                    if(i==0)    {
                    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 
                    }
                    googleMap.addMarker(markerOptions);


               }
             }
           }
        }

i think error in this line

  addresses = geocoder.getFromLocationName(locationName[0], 3);

address dosent receive anything ....thx in advance...help me friends

Tufan
  • 2,789
  • 4
  • 34
  • 52
  • Why you not use Longitude and Latitude for this?? – Chirag Savsani Mar 25 '15 at 08:04
  • @chirag how to use latitude and longitude in this activity – Tufan Mar 25 '15 at 08:08
  • my question is that why address receive null value......when i m sending it – Tufan Mar 25 '15 at 08:25
  • use this addresses = geocoder.getFromLocation(latitude, longitude, 1); – Chirag Savsani Mar 25 '15 at 08:52
  • and you can get longitude and latitude like this.... Location location = googleMap.getMyLocation(); // latitude = location.getLatitude(); // longitude = location.getLongitude(); – Chirag Savsani Mar 25 '15 at 08:55
  • @chirag you forget i m reciving in protected List
    doInBackground(String... locationName) { so how we can get location lattitude and lattitude
    – Tufan Mar 25 '15 at 08:58
  • I know that, But I only suggest you that you can store longitude and latitude when you getting address... and after that you can easily use longitude and latitude for set pin at particular address.. – Chirag Savsani Mar 25 '15 at 09:15

1 Answers1

0

In my App I use This code to get Address...!!! This is for your reference.

Geocoder geocoder;
    List<Address> addresses;
    double latitude, longitude;
    String zip, city, state, country;
    googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

                @Override
                public void onMapLongClick(LatLng arg0) {

                    latitude = arg0.latitude;
                    longitude = arg0.longitude;
                    String title = "";
                    geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
                    try {
                        addresses = geocoder.getFromLocation(latitude, longitude, 1);
                        if (addresses != null && addresses.size() > 0) {
                            zip = addresses.get(0).getPostalCode();
                            city = addresses.get(0).getLocality();
                            state = addresses.get(0).getAdminArea();
                            country = addresses.get(0).getCountryName();
                            if (zip != null) {
                                title += zip + ",";
                            }
                            if (city != null) {
                                title += city + ",";
                            }
                            if (state != null) {
                                title += state + ",";
                            }
                            if (country != null) {
                                title += country;
                            }
                        } else {
                            title = "Unknown Location";
                            showPosition.setText("Address Not Found");
                        }

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    // This will put marker and set Address as a marker title
                    googleMap.addMarker(new MarkerOptions().position(arg0).title(title));


                }
            }); 


how to set marker in google map?

 MarkerOptions options = new MarkerOptions().position(latLng).title(shortDescStr);
                googleMap.addMarker(options);
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
  • i know this can be correct but it dosent help me because i want to set marker by default ..i m fetching value from json and pass to Geocoder – Tufan Mar 25 '15 at 07:52
  • but why adresss reciieve null value.....i send defalut value from new GeocoderTask(MainActivityMap.this).execute(location); and in location i set default value – Tufan Mar 25 '15 at 07:53