Google Maps Android API v2不显示标记标题

时间:2014-05-29 09:42:37

标签: android google-maps-android-api-2

我尝试了一些代码来制作地理编码反向位置(使用Google Maps Android API v2)并使用标记显示标题,但是当我运行应用程序时标记标题没有显示。 这是我的代码:

public class MainActivity extends FragmentActivity {

GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SupportMapFragment supportMapFragment = (SupportMapFragment)
            getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting a reference to the map
    googleMap = supportMapFragment.getMap();

    // Setting a click event handler for the map
    googleMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {

            // Getting the Latitude and Longitude of the touched location
            latLng = arg0;

            // Clears the previously touched position
            googleMap.clear();

            // Animating to the touched position
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

            // Creating a marker
            markerOptions = new MarkerOptions();

            // Setting the position for the marker
            markerOptions.position(latLng);

            // Placing a marker on the touched position
            googleMap.addMarker(markerOptions);

            // Adding Marker on the touched location with address
            new ReverseGeocodingTask(getBaseContext()).execute(latLng);

        }
    });

}

private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
    Context mContext;

    public ReverseGeocodingTask(Context context){
        super();
        mContext = context;
    }

    // Finding address using reverse geocoding
    @Override
    protected String doInBackground(LatLng... params) {
        Geocoder geocoder = new Geocoder(mContext);
        double latitude = params[0].latitude;
        double longitude = params[0].longitude;

        List<Address> addresses = null;
        String addressText="";

        try {
            addresses = geocoder.getFromLocation(latitude, longitude,1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(addresses != null && addresses.size() > 0 ){
            Address address = addresses.get(0);

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

        return addressText;

    }

    @Override
    protected void onPostExecute(String addressText) {
        // Setting the title for the marker.
        // This will be displayed on taping the marker
        markerOptions.title(addressText);

        // Placing a marker on the touched position
        googleMap.addMarker(markerOptions);

    }
  } 
}

我的代码有问题吗?

2 个答案:

答案 0 :(得分:2)

onPostExecute()

中更改您的代码
googleMap
.addMarker(
        new MarkerOptions()
                .position(loc)
                .draggable(true)                                
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .title(addressText))
.showInfoWindow();

答案 1 :(得分:1)

试试这个

public class MainActivity extends FragmentActivity {

    GoogleMap googleMap;
    MarkerOptions markerOptions;
    LatLng latLng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SupportMapFragment supportMapFragment = (SupportMapFragment)
                getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting a reference to the map
        googleMap = supportMapFragment.getMap();

        // Setting a click event handler for the map
        googleMap.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng arg0) {

                // Getting the Latitude and Longitude of the touched location
                latLng = arg0;

                // Clears the previously touched position
                googleMap.clear();

                // Animating to the touched position
                googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                // Creating a marker
                markerOptions = new MarkerOptions();

                // Setting the position for the marker
                markerOptions.position(latLng);

                // Placing a marker on the touched position
                googleMap.addMarker(markerOptions);

                // Adding Marker on the touched location with address
                new GetAddressTask().execute(latLng);
                // new ReverseGeocodingTask(MainActivity.this).execute(latLng);

            }
        });
    }

    public class GetAddressTask extends AsyncTask<LatLng, Void, Integer>{
        private LatLng loc;
        String addressText;
        @Override
        protected Integer doInBackground(LatLng... params) {
            int mFinalFlag=0;
            loc=params[0];
            String filterAddress = "";

            Geocoder geoCoder = new Geocoder(MainActivity.this, Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(loc.latitude,
                        loc.longitude, 1);

                if (addresses!=null&&addresses.size() > 0) {
                    Address address = addresses.get(0);
                    addressText = String.format(
                        "%s, %s, %s",
                        address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                        address.getLocality(),
                        address.getCountryName());
                }
            } catch (IOException ex) {
            } catch (Exception e2) {            
                e2.printStackTrace();
            }
            return mFinalFlag;
        }
    }

    @Override
    protected void onPostExecute(Integer result) {
        googleMap.addMarker(
            new MarkerOptions()
                .position(loc)
                .draggable(true)                                
                .icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .title(addressText))
            .showInfoWindow();

        super.onPostExecute(result);
    }
}