2017-07-30 4 views
0

私のアプリにはGoogleマップが含まれており、forループを使用して地図に4つのマーカーが追加されています。ジオコーダーを使用して、郵便番号、市の名前、追加されたマーカーの住所も取得できました。マーカータップは必ずしも認識されません

これはすべて動作しますが、マーカーのクリックが常に機能するとは限りません。時々、マーカーのタイトルを見るためにダブルタップをしなければならない。私は本当に理由を知らない。ここに私の完全なコード

 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

は、私は、その作業

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



    } 

} 
をテストしている、このコードを試しています
関連する問題