2014-10-01 1 views
1

메신저를 Google지도에로드하는 중입니다.지도 및 40 개의 마커를로드하는 데 약 5 초가 걸립니다. (지도 및 마커가로드되기 전에 화면이 비어 있음)Google지도 표시자를 더 빠르게로드하는 방법은 무엇입니까?

다음은 내 xml 파일입니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <fragment 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      class="com.google.android.gms.maps.SupportMapFragment"/> 
</RelativeLayout> 

다음 코드는지도에 마커를 추가 :

1), 모든 레스토랑 객체를 반복 각 레스토랑에 대한 주소를 가져옵니다.

2) LatLng를에 주소가

3) 마커를 추가하는 방법 AsyncTaskonPostExecute를 사용

 //partial of the method 
     for (Restaurant rest : rests) { 
      LatLng ll = getLatLng(rest.getAddress());//converts address to LatLng 
      MarkerOptions marker = new MarkerOptions() 
        .title(rest.getName()) 
//     .snippet("Briefly description: " + i++) 
        .position(ll) 
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)) 
        .anchor(0.0f, 1.0f); 
      myMap.addMarker(marker); 
     } 


     public LatLng getLatLng(String address) { 

     try { 
      ArrayList<Address> addresses = (ArrayList<Address>) geocoder.getFromLocationName(address, MAX_ADDRESS_ALLOWDED); 
      for (Address add : addresses) { 
       if (true) {//Controls to ensure it is right address such as country etc. 
        longitude = add.getLongitude(); 
        latitude = add.getLatitude(); 
       } 
      } 
     } catch (Exception e) { 
      Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
     return new LatLng(latitude, longitude); 
    } 

임 마커에 추가 개체를 변환, 누구든지 나를 도울 수 있습니까?

+1

매번로드하지 못하도록 위 주소를 찾아 본 후 위도와 경도를 캐싱하도록 제안합니다. 지오 코더 요청이 차단되어 오랜 시간이 걸릴 수 있습니다. – kcoppock

답변

0

AsyncTask의 onPostExecute 메서드는 기본 UI 스레드에서 실행됩니다. 어떤 이유로 (시도한 경우) 포스트에서 토스트를 만들 수 있지만 백그라운드 스레드에서 토스트를 만들 수는 없습니다. 기본적으로 부하의 일부를 주요 활동에서 제거하여 부하를 제거하려고합니다. 특히 백그라운드에서 루프를 유지하십시오. 새 스레드를 사용하고 처리기로 마커를 배치하십시오.

public void placeMarkers(final ArrayList<String>myArray, final Handler handler){ 
    new Thread(){ 
     @Override 
     public void run(){ 
     //my for loops 
      for(int i = 0; i < myArray.size(); i++){ 
       //post with a handler.. 

       handler.post(new Runnable() { 
        @Override 
        public void run() { 
         //place your markers. 
        } 
       }); 
      } 
      //etc etc.. 
     } 
    }.run(); 
} 

또 다른 방법은 .. 스레드 때문에 핸들러에 대한 필요성을 제거하여 MainActivity 사이의 인터페이스를 사용하는겠습니까 다른 당김 폴더

1

또한 퍼팅 마커 '아이콘 (hdpi에, MDPI ,. ..) 대신 하나의 폴더 (drawable) 도움이 될 수 있습니다. 나는 아이콘을 다른 화면 해상도와 호환되게 만드는 것을 의미합니다.

관련 문제