2012-05-25 3 views
3

나는 ASyncTask에서 사용자 위치를 결정한 후 맵의 특정 위치에 마커를 추가하는 맵보기를 가지고 있습니다. 위치를 찾은 후에지도를 업데이트 할 수 없습니다. onPostExecute를 실행하기 전에 위치를 찾을 때까지 기다릴 수있는 방법이 있습니까? ASyncTask를 사용하지 않고 MainMapView 클래스에 위치 수신기를 포함 시키려고했습니다. 이렇게하면지도가 업데이트되지만지도가 실제로 느리고 지체됩니다. 나는 이것이 새로운 위치가 발견 될 때마다지도가 업데이트된다는 사실에 기인한다고 생각합니다. 어떤 도움을 많이 주시면 감사하겠습니다. 갱신, 다음 코드를 작동하지만지도로ASync가있는 안드로이드 업데이트 맵 위치

import android.os.Bundle; 
import android.os.AsyncTask; 
import android.os.Looper; 
import android.util.Log; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapController; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 


public class MainMapView extends MapActivity{ 

    private Location currentLocation; 
    private String serviceName; 
    private MapController mapController; 
    private List<Overlay> mapOverlays; 
    private ItemizedOverlay itemizedoverlay; 
    private LocationManager locationManager; 
    private HealthCarePractice[] practices; 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_map_view); 

     Bundle retItem = getIntent().getExtras(); 
     serviceName = retItem.getString("serviceName"); 
     //Log.e("This One", serviceName); 

     MapView mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     //mapView.setSatellite(true); 
     mapController = mapView.getController(); 

     mapOverlays = mapView.getOverlays(); 
     Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker); 
     itemizedoverlay = new ItemizedOverlay(drawable, this); 

     Context context = this; 

     MainMapViewTask task = new MainMapViewTask(); 
     task.execute(context); 

    } 

    public class MainMapViewTask extends AsyncTask<Context, Integer, Void> 
    { 
     Context localContext; 

     @Override 
     protected Void doInBackground(Context... params) { 
      localContext = params[0]; 
      // Aquire a reference to the system Location Manager 
      locationManager = (LocationManager) localContext.getSystemService(Context.LOCATION_SERVICE); 

      // Define a listener that responds to location updates 
      LocationListener locationListener = new LocationListener() { 
       public void onLocationChanged(Location location) { 
        // Called when a new location is found by the network location provider. 
        if (location != null) 
        { 
         currentLocation = location; 
         locationManager.removeUpdates(this); 
         locationManager = null; 
         Geocoder geocoder = new Geocoder(MainMapView.this, Locale.getDefault()); 

         List<Address> list; 

         if(currentLocation == null) 
         { 
          Log.e("Message", "Location not found"); 
         }else{ 
          try { 
           list = geocoder.getFromLocation(
           currentLocation.getLatitude(), currentLocation.getLongitude(), 1); 
           if (list != null && list.size() > 0) { 
            android.location.Address address = list.get(0); 
            //Log.e("Post Code", address.getPostalCode()); 
            String poCode = address.getPostalCode(); 
            if (poCode != null) 
            { 
             //Log.e("Post Code", address.getPostalCode()); 
             String searchString = buildSearchString(serviceName, poCode.replaceAll(" ", "")); 
             //Log.e("posplit", poCode.split(" ")[0]); 
             Log.e("Search String", searchString); 
             RemoteData remoteData = new RemoteData("Location", searchString); 
             practices = remoteData.getPractices(); 
            } 
           } 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 

       } 

       public void onStatusChanged(String provider, int status, Bundle extras) {} 

       public void onProviderEnabled(String provider) {} 

       public void onProviderDisabled(String provider) {} 
       }; 

      Looper.myLooper(); 
      Looper.prepare(); 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      if(currentLocation != null) 
      { 
       GeoPoint currentPoint = new GeoPoint((int)(currentLocation.getLatitude()*1000000), (int)(currentLocation.getLongitude()*1000000)); 
       mapController.setCenter(currentPoint); 
       mapController.setZoom(15); 
       for(int i=0; i< practices.length; i++) 
       { 
        int latitude = (int)(practices[i].getLatitude()*1000000); 
        int longitude = (int)(practices[i].getLongitude()*1000000); 
        currentPoint = new GeoPoint(latitude, longitude); 
        mapController.setCenter(currentPoint); 
        mapController.setZoom(15); 
        String[] addressLines = practices[i].getAddress().getAddressLines(); 
        StringBuilder sb = new StringBuilder(); 
        for(int y=0; y<addressLines.length; y++) 
        { 
         sb.append(addressLines[y]); 
         sb.append('\n'); 
        } 
        sb.append(practices[i].getAddress().getPostcode()); 
        sb.append('\n'); 
        sb.append("Telephone: "); 
        sb.append(practices[i].getTelephone()); 
        OverlayItem currentOverlayItem = new OverlayItem(currentPoint,practices[i].getTitle(),sb.toString()); 
        itemizedoverlay.addOverlay(currentOverlayItem); 
        mapOverlays.add(itemizedoverlay); 
       } 
      } 
     } 

    } 
} 

은 사용자가

import android.os.Bundle; 
import android.os.AsyncTask; 
import android.os.Looper; 
import android.util.Log; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 


import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapController; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 


public class MainMapView extends MapActivity{ 

    private Location currentLocation; 
    private String serviceName; 
    private MapController mapController; 
    private List<Overlay> mapOverlays; 
    private ItemizedOverlay itemizedoverlay; 
    private LocationManager locationManager; 
    private HealthCarePractice[] practices; 
    private boolean mapDrawn = false; 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_map_view); 

     Bundle retItem = getIntent().getExtras(); 
     serviceName = retItem.getString("serviceName"); 
     //Log.e("This One", serviceName); 

     MapView mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     //mapView.setSatellite(true); 
     mapController = mapView.getController(); 

     mapOverlays = mapView.getOverlays(); 
     Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker); 
     itemizedoverlay = new ItemizedOverlay(drawable, this); 

     Context context = this; 

     /* 
     MainMapViewTask task = new MainMapViewTask(); 
     task.execute(context); 
     */ 

     locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     // Define a listener that responds to location updates 
     LocationListener locationListener = new LocationListener() { 
      public void onLocationChanged(Location location) { 
       // Called when a new location is found by the network location provider. 
       if (location != null) 
       { 
        currentLocation = location; 
        locationManager.removeUpdates(this); 
        locationManager = null; 
        Geocoder geocoder = new Geocoder(MainMapView.this, Locale.getDefault()); 

        List<Address> list; 

        if(currentLocation == null) 
        { 
         Log.e("Message", "Location not found"); 
        }else{ 
         try { 
          list = geocoder.getFromLocation(
          currentLocation.getLatitude(), currentLocation.getLongitude(), 1); 
          if (list != null && list.size() > 0) { 
           android.location.Address address = list.get(0); 
           //Log.e("Post Code", address.getPostalCode()); 
           String poCode = address.getPostalCode(); 
           if (poCode != null) 
           { 
            //Log.e("Post Code", address.getPostalCode()); 
            String searchString = buildSearchString(serviceName, poCode.replaceAll(" ", "")); 
            //Log.e("posplit", poCode.split(" ")[0]); 
            Log.e("Search String", searchString); 
            RemoteData remoteData = new RemoteData("Location", searchString); 
            practices = remoteData.getPractices(); 
            if(!mapDrawn) 
            { 
             mapDrawn = true; 
             if(currentLocation != null) 
             { 
              GeoPoint currentPoint = new GeoPoint((int)(currentLocation.getLatitude()*1000000), (int)(currentLocation.getLongitude()*1000000)); 
              mapController.setCenter(currentPoint); 
              mapController.setZoom(15); 
              for(int i=0; i< practices.length; i++) 
              { 
               int latitude = (int)(practices[i].getLatitude()*1000000); 
               int longitude = (int)(practices[i].getLongitude()*1000000); 
               currentPoint = new GeoPoint(latitude, longitude); 
               mapController.setCenter(currentPoint); 
               mapController.setZoom(15); 
               String[] addressLines = practices[i].getAddress().getAddressLines(); 
               StringBuilder sb = new StringBuilder(); 
               for(int y=0; y<addressLines.length; y++) 
               { 
                sb.append(addressLines[y]); 
                sb.append('\n'); 
               } 
               sb.append(practices[i].getAddress().getPostcode()); 
               sb.append('\n'); 
               sb.append("Telephone: "); 
               sb.append(practices[i].getTelephone()); 
               OverlayItem currentOverlayItem = new OverlayItem(currentPoint,practices[i].getTitle(),sb.toString()); 
               itemizedoverlay.addOverlay(currentOverlayItem); 
               mapOverlays.add(itemizedoverlay); 
              } 
             } 
            } 
           } 
          } 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 

      } 

      public void onStatusChanged(String provider, int status, Bundle extras) {} 

      public void onProviderEnabled(String provider) {} 

      public void onProviderDisabled(String provider) {} 
      }; 

     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

    } 

답변

0

새 위치로 드래그하여지도와 상호 작용하려고 시도 할 때 지연이 매우 랙이있다 나는 단지 그림자가 각 마크에 대해 만들어지기 때문에 맵이 뒤떨어져 있다는 것을 알았습니다. 왜 이것이 발생했는지 모르지만, OverlayItem 클래스에서 다음 코드를 사용하여 마커의 그림자를 제거하면 내 문제가 해결되었습니다.

@Override 
public void draw(Canvas canvas, MapView mapView, boolean shadow) 
{ 
    if(!shadow) 
    { 
     super.draw(canvas, mapView, false); 
    } 
} 

마커의 그림자는 끔찍한 위치를 벗어났습니다. 누군가가 정확한 위치에있는 그림자를 통합하는 해결책을 가지고 있다면 알려주십시오. 감사합니다 Kush