GeocoderTask错误

时间:2012-11-30 09:55:39

标签: android google-maps geocoding

我正在尝试关注一些有关地理编码器的项目并将其集成到我的项目中。 我在行new GeocoderTask().execute(hosname);中执行了一个redline,但是我用作引用的现有项目不包含这种错误。当我按ctr + 1时显示3个选项:
1:更改为on PostExcute
2.更改hosname的类型为String []
3.创建方法在Geocoder类型中执行'Textview'任务。
我使用的现有项目是使用编辑文本来获取要显示的地址的名称,而我的项目是从textview引用它。我该怎么办呢?

package com.dr.droid.lee;

import java.io.IOException;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class HospResult extends MapActivity {

    String hcity;
    String hregion;
    String hadd;
    String hname;
    String hcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Typeface type = Typeface.createFromAsset(this.getAssets(),"MyriadPro-Bold.ttf");
        setContentView(R.layout.map);
        TextView hosname =  (TextView) findViewById(R.id.tvName);
        TextView hosreg =  (TextView) findViewById(R.id.tvRegion);
        TextView hosadd =  (TextView) findViewById(R.id.tvAddress);
        TextView hoscon =  (TextView) findViewById(R.id.tvContact);
        TextView hoscity =  (TextView) findViewById(R.id.tvCity);
        Bundle receive = getIntent().getExtras();
        hcity = receive.getString("city");
        hregion = receive.getString("region");
        hadd = receive.getString("address");
        hname = receive.getString("name");
        hcon = receive.getString("contact");
        hosname.setText(hname);
        hosreg.setText(hregion);
        hoscity.setText(hcity);
        hosadd.setText(hcon);
        hoscon.setText(hadd);
        hosname.setTypeface(type);
        hosreg.setTypeface(type);
        hosadd.setTypeface(type);
        hoscity.setTypeface(type);
        hoscon.setTypeface(type);
        MapView mv =  (MapView) findViewById(R.id.mv1);
        MapController mp = mv.getController();
        mp.setZoom(15);
        if(hosname!=null && !hosname.equals("")){
            new GeocoderTask().execute(hosname);
        }


    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    // An AsyncTask class for accessing the GeoCoding Web Service
        private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{

            @Override
            protected List<Address> doInBackground(String... locationName) {
                // Creating an instance of Geocoder class
                Geocoder geocoder = new Geocoder(getBaseContext());
                List<Address> addresses = null;

                try {
                    // Getting a maximum of 3 Address that matches the input text
                    addresses = geocoder.getFromLocationName(locationName[0], 3);
                } catch (IOException e) {
                    e.printStackTrace();
                }           
                return addresses;
            }


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

                // Getting Reference to MapView of the layout activity_main
                MapView mapView = (MapView) findViewById(R.id.mv1);

                // Setting ZoomControls
                mapView.setBuiltInZoomControls(true);

                // Getting MapController for the MapView
                MapController mc = mapView.getController();
                mc.setZoom(18);

                // Getting Drawable object corresponding to a resource image
                Drawable drawable = getResources().getDrawable(R.drawable.marker);


                // Getting Overlays of the map
                List<Overlay> overlays = mapView.getOverlays();

                // Creating an ItemizedOverlay
                LocationOverlay locationOverlay = new LocationOverlay(drawable,getBaseContext());

                // Clearing the overlays
                overlays.clear();

                if(addresses==null || addresses.size()==0){
                    Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();

                    // Redraws the map to clear the overlays
                    mapView.invalidate();
                }

                // Adding Markers on Google Map for each matching address
                for(int i=0;i<addresses.size();i++){                

                    Address address = (Address) addresses.get(i);

                    // Creating an instance of GeoPoint, to display in Google Map
                    GeoPoint p = new GeoPoint(
                                              (int)(address.getLatitude()*1E6),
                                              (int)(address.getLongitude()*1E6)
                                             );

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


                    // Creating an OverlayItem to mark the point
                    OverlayItem overlayItem = new OverlayItem(p, "Location",addressText);

                    // Adding the OverlayItem in the LocationOverlay
                    locationOverlay.addOverlay(overlayItem);

                    // Adding locationOverlay to the overlay
                    overlays.add(locationOverlay);

                    // Locate the first location
                    if(i==0)
                        mc.animateTo(p);                    
                }

                // Redraws the map
                mapView.invalidate();

            }       
        }

}

1 个答案:

答案 0 :(得分:1)

类型hosname是TextView,也许这会导致错误?

尝试执行new GeocoderTask().execute(hosname.getText());