2016-09-26 5 views
0

여기 상황이 있습니다. 나는 경도와 위도가 1 떨어져있을 때만 나타나는 마커를 만들려고합니다. 또한 LatLngBounds를지도에서 처음부터 1 마디 내에 마커에 맞게 만들려고합니다.Google지도 마커 설정 및 경계 지정 - Android

나는 모든 것을 작동시킬 수는 있지만 동시에 할 수는 없습니다. If/Else 문에 잘못된 것이 있습니다.

protected void onPostExecute(Address address) { 
     mMap.clear(); 
     if (address == null) { 
      Toast.makeText(MainActivity.this, R.string.invalid_entry, Toast.LENGTH_LONG).show(); 
     } else { 
      String addressName = ""; 
      for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { 
       addressName += " - " + address.getAddressLine(i); 
      } 

      Double longitude = address.getLongitude(); 
      Double latitude = address.getLatitude(); 

      LatLng position = new LatLng(address.getLatitude(), address.getLongitude()); 

      LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
      builder.include(position); 
      builder.include(position); 

      //search location 
      mMap.addMarker(new MarkerOptions().position(position).title(addressName) 
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))); 

      for (int i = 0; i < markers.size(); i++) { 
       Double distanceLng = Math.abs(markers.get(i).longitude - longitude); 
       Double distanceLat = Math.abs(markers.get(i).latitude - latitude); 
       if ((distanceLng < 1) && (distanceLat < 1)) { 
        mMap.addMarker(new MarkerOptions() 
          .position(markers.get(i)) 
          .title("Place") 
          .snippet("Description")); 
        builder.include(markers.get(i)); 
        LatLngBounds bounds = builder.build(); 
        int padding = 50; // offset from edges of the map in pixels 
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding); 
        Log.d(TAG, "DistanceBounds: " + bounds); 
        Log.d(TAG, "DistancePadding: " + padding); 
        Log.d(TAG, "DistanceI: " + i); 
        mMap.moveCamera(cu); 
       } 
       else { 
        Toast.makeText(MainActivity.this, R.string.no_places_in_area,Toast.LENGTH_LONG); 
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 10)); 
        Log.d(TAG, "DistanceLng Failed = " + distanceLng + "i = " + i); 
        Log.d(TAG, "DistanceLat Failed = " + distanceLat + "i = " + i); 
       } 
      } 


     } 

올바른 코드 작성에 도움을 주시면 감사하겠습니다!

+0

Whare는 builder.include (position);을 두 번 입력했는데 두 번째는 대개 오타입니다 –

+0

또한 '1 경도와 위도가 어둡습니다'라고 말한 경우 distanceLng에서 distanceLng을 빼야합니다. –

답변

0

주어진 숫자의 절대 값을 반환하는 Math.abs() 메서드를 호출합니다. 즉, Math.abs (-10)는 10을 반환합니다. Math.floor 또는 Math.ceil을 per로 사용할 것을 제안합니다. 귀하의 요구 사항.

System.out.println(Math.ceil(25.46)); // Prints 26 
System.out.println(Math.floor(25.46)); // Prints 25 

이렇게하면 정확한 빼기 값을 얻을 수 있습니다. 위도와 경도의 값이 항상 두 배이므로 항상 요구 사항에 따라 정확한 값 1을 얻지 못할 수도 있습니다.