在 Geocoder Asyncktask 中找不到位置 Activity

No Location Found In Geocoder Asyncktask Activity

我想构建一个应用程序来显示 google 地图上的用户位置...但它显示没有找到地址..即使我试图给出固定值...

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

                }

我的地理编码器异步任务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);


               }
             }
           }
        }

我认为这一行有误

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

地址没有收到任何东西 ....提前谢谢...帮助我朋友

在我的应用程序中,我使用此代码获取地址...!!!供大家参考。

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));


                }
            }); 


如何在 google 地图中设置标记?

 MarkerOptions options = new MarkerOptions().position(latLng).title(shortDescStr);
                googleMap.addMarker(options);