在Geocoder Asynctask活动中找不到位置

时间:2015-03-25 06:59:40

标签: android google-maps android-activity android-asynctask android-location

我想构建一个应用程序,向我显示谷歌地图上的用户位置...但它显示我找不到地址..即使我试图给出固定价值......

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

                }

我的Geocoder Asynctask活动

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

地址剂量接收任何东西 ....提前thx ...帮助我的朋友们

1 个答案:

答案 0 :(得分:0)

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

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


                }
            }); 


 如何在谷歌地图中设置标记?

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