标记点击并不总是被识别为android

时间:2017-07-30 14:14:52

标签: android google-maps

我在我的应用中添加了Google地图,并使用for循环在地图中添加了4个标记。我还设法通过使用Geocoder获取邮政编码,城市名称和这些添加标记的地址。

一切正常,但问题是标记上的点击似乎并不总是有效。有时我必须双击才能看到标记的标题。我真的不知道为什么。这是我的完整代码

     public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    String city, adress, zip;
    Marker marker;
    LatLngBounds.Builder builder = new LatLngBounds.Builder();

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

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;    

        LatLng[] point_new = new LatLng[4];
        point_new[0] = new LatLng(52.4788535, 13.32730760000004);
        point_new[1] = new LatLng(52.4794297, 13.313520799999992);
        point_new[2] = new LatLng(52.5272885, 13.458033200000045);
        point_new[3] = new LatLng(52.52603999999999, 13.488159999999993);

        for (int i = 0; i < point_new.length; i++) {
            drawMarker(point_new[i]);

                  marker = mMap.addMarker(new MarkerOptions().position(point_new[i]));

           mMap.moveCamera(CameraUpdateFactory.newLatLng(point_new[i]));


        }
        getAdress(52.4788535, 13.32730760000004 );
        marker = mMap.addMarker(new MarkerOptions().position(point_new[0]).title(adress+"," + zip + "" + city));

        getAdress(52.4794297, 13.313520799999992);
        marker = mMap.addMarker(new MarkerOptions().position(point_new[1]).title(adress+"," + zip + "" + city));

        getAdress(52.5272885, 13.458033200000045);
        marker = mMap.addMarker(new MarkerOptions().position(point_new[2]).title(adress+"," + zip + "" + city));

        getAdress(52.52603999999999, 13.488159999999993);
         marker = mMap.addMarker(new MarkerOptions().position(point_new[3]).title(adress+"," + zip + "" + city));

        for (int j = 0; j < point_new.length; j++) {
            builder.include(point_new[j]);    
        }


        LatLngBounds bounds = builder.build();
        mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));

    }


    public void drawMarker(LatLng point) {
        // Creating an instance of MarkerOptions
        MarkerOptions markerOptions = new MarkerOptions();

        // Setting latitude and longitude for the marker
        markerOptions.position(point);

        // Adding marker on the Google Map
        mMap.addMarker(markerOptions);
    }

    public void getAdress(double lat, double lng){
        Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());

        List<Address> addresses  = null;
        try {

            addresses = geocoder.getFromLocation(lat,lng,1);

        } catch (IOException e) {
            e.printStackTrace();
        }

        city = addresses.get(0).getLocality();
         adress = addresses.get(0).getAddressLine(0);
         zip = addresses.get(0).getPostalCode();
    }
}

1 个答案:

答案 0 :(得分:1)

  

尝试此代码,我已经测试过,它的工作

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        List<MarkerDesc> markerDescList = new ArrayList<>();
        markerDescList.add(new MarkerDesc(new LatLng(52.4788535, 13.32730760000004), getAdress(52.4788535, 13.32730760000004)));
        markerDescList.add(new MarkerDesc(new LatLng(52.4794297, 13.313520799999992), getAdress(52.4794297, 13.313520799999992)));
        markerDescList.add(new MarkerDesc(new LatLng(52.5272885, 13.458033200000045), getAdress(52.5272885, 13.458033200000045)));
        markerDescList.add(new MarkerDesc(new LatLng(52.52603999999999, 13.488159999999993), getAdress(52.52603999999999, 13.488159999999993)));

        for(int i=0; i<markerDescList.size(); i++){
            MarkerDesc markerDesc = markerDescList.get(i);

            mMap.addMarker(new MarkerOptions()
                    .position(markerDesc.getLatLng())
                    .title(markerDesc.getAddresses().get(0).getLocality())
                    .snippet(markerDesc.getAddresses().get(0).getAddressLine(0)+"\n"+markerDesc.getAddresses().get(0).getPostalCode()+"\n"+markerDesc.getAddresses().get(0).getLocality())
                    .icon(BitmapDescriptorFactory.defaultMarker()));
        }
        LatLng latLng = markerDescList.get(markerDescList.size()-1).getLatLng();
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(latLng).zoom(15).bearing(0).tilt(25).build()));


    }


    public List<Address> getAdress(double lat, double lng){
        Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());
        try {
            return geocoder.getFromLocation(lat,lng,1);

        } catch (IOException e) {
            e.printStackTrace();
        }

        /*String city = addresses.get(0).getLocality();
        String adress = addresses.get(0).getAddressLine(0);
        String zip = addresses.get(0).getPostalCode();*/

        return  null;
    }


    private class MarkerDesc{
        LatLng latLng;
        List<Address> addresses;

        private MarkerDesc(LatLng ltLng, List<Address> addr){
            this.latLng=ltLng;
            this.addresses = addr;
        }

        private LatLng getLatLng() {
            return latLng;
        }

        private List<Address> getAddresses() {
            return addresses;
        }



    }

}