【问题标题】:Google Maps Android API v2 don't show marker titleGoogle Maps Android API v2 不显示标记标题
【发布时间】:2014-05-29 09:42:36
【问题描述】:

我尝试了一些代码来进行地理编码反向定位(使用 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);

    }
  } 
}

我的代码有问题吗?

【问题讨论】:

  • 您添加了两次标记,一次是在单击地图时,一次是在地理编码完成后。您应该只在地理编码完成后添加它。并且不要为 markerOptions 保留类成员。在 onPostCreate 本地重新创建它。我觉得会更好。

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


【解决方案1】:

更改onPostExecute()中的代码

googleMap
.addMarker(
        new MarkerOptions()
                .position(loc)
                .draggable(true)                                
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .title(addressText))
.showInfoWindow();

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多