程序响应真的很慢[Google Api]

时间:2012-06-18 22:52:05

标签: android eclipse web-services google-maps

我是Android编程的新手,我正在尝试开发这个应用程序来调用Android手机上的谷歌地图服务,然后访问一个网络服务来检索需要在其上绘制的站点的纬度和经度值地图。

Foll是代码,但是当我运行应用程序时,程序没有响应,它只绘制一个声明,而且平移和缩放事件也很慢,有时候代码卡住了。

请帮助解决方案。

OpenMap.java


public class OpenMap extends MapActivity 
{

private MapController mapController;
private MyLocationOverlay myLocation;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get Mapping Controllers etc
    MapView mapView = (MapView) findViewById(R.id.map_view);
    mapController = mapView.getController();
    mapController.setZoom(1);
    mapView.setBuiltInZoomControls(true);

    // Add the MyLocationOverlay
    myLocation = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLocation);

    myLocation.enableMyLocation();

    DemoOverlay demoOverlay = new DemoOverlay();
    mapView.getOverlays().add(demoOverlay);
    myLocation.runOnFirstFix(new Runnable() {
        public void run() {
            mapController.animateTo(myLocation.getMyLocation());
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    myLocation.enableMyLocation();
}

@Override 
protected void onPause() {
    super.onPause();
    myLocation.disableMyLocation();
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

DemoOverlay.java

public class DemoOverlay extends Overlay
{
ArrayList<String> arrlat_long = new ArrayList<String>();
public static String str;
public static String str1;
String responseBody;
int n=0;
 String Url =   "http://hiscentral.cuahsi.org/webservices/hiscentral.asmx/GetSeriesCatalogForBox2";
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5); 
 HttpClient httpclient = new DefaultHttpClient();   
   @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
   {
        super.draw(canvas, mapView, shadow);
        Projection projection = mapView.getProjection();
         if(!Url.endsWith("?"))
         {
                Url += "?";
         }

            int latSpan = mapView.getLatitudeSpan();
            int lngSpan = mapView.getLongitudeSpan();
            GeoPoint mapCenter = mapView.getMapCenter();
            int mapLeftGeo = mapCenter.getLongitudeE6() - (lngSpan / 2);
            int mapRightGeo = mapCenter.getLongitudeE6() + (lngSpan / 2);

            int mapTopGeo = mapCenter.getLatitudeE6() - (latSpan / 2);
            int mapBottomGeo = mapCenter.getLatitudeE6() + (latSpan / 2);

            GeoPoint geoPoint = this.getSampleLocation();


         nameValuePairs.add(new BasicNameValuePair("xmin", "-100"));
         nameValuePairs.add(new BasicNameValuePair("xmax", "-90"));
         nameValuePairs.add(new BasicNameValuePair("ymin", "40"));
         nameValuePairs.add(new BasicNameValuePair("ymax", "55"));
         nameValuePairs.add(new BasicNameValuePair("networkIDs", ""));
         nameValuePairs.add(new BasicNameValuePair("conceptKeyword", "precipitation"));
         nameValuePairs.add(new BasicNameValuePair("beginDate", "1/1/2009"));
         nameValuePairs.add(new BasicNameValuePair("endDate", "1/1/2010"));
         String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
         Url += paramString;

  if ((geoPoint.getLatitudeE6() > mapTopGeo && geoPoint.getLatitudeE6() < mapBottomGeo)
                 && (geoPoint.getLongitudeE6() > mapLeftGeo && geoPoint.getLongitudeE6() < mapRightGeo)) 
              {
                    geoPoint = arrlat_long(n);
                    Point myPoint = new Point();
                    projection.toPixels(geoPoint, myPoint);

                    Bitmap marker = BitmapFactory.decodeResource(mapView.getContext().getResources(), R.drawable.markerblue);

                    canvas.drawBitmap(marker, myPoint.x - 15, myPoint.y - 30, null);
                }

   }

private GeoPoint getSampleLocation() 
    {
       GeoPoint sampleGeoPoint;
      // int n=0;

       try {
              HttpPost httppost = new HttpPost(Url); 
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              HttpResponse response = httpclient.execute(httppost);
              responseBody = EntityUtils.toString(response.getEntity());

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setValidating(false);
            XmlPullParser myxml = factory.newPullParser();
            InputStream raw = new ByteArrayInputStream(responseBody.getBytes());
            myxml.setInput(raw, null);

            int eventType = myxml.getEventType();
            while(eventType != XmlPullParser.END_DOCUMENT) 
            {
                if(eventType == XmlPullParser.START_DOCUMENT) 
                {

                    Log.d("ParseXmlActivity", "In start document");
                }
                else if(eventType == XmlPullParser.START_TAG) 
                {
                    if (myxml.getName().equals("latitude"))
                    {
                        str="";
                        str  += "Lat:" + myxml.nextText().toString();
                         Log.d("ParseXmlActivity", "In start tag = "+str);
                         arrlat_long.add(str);
                    }
                    if (myxml.getName().equals("longitude"))
                    {
                        str1 += "--Lon:" + myxml.nextText().toString();
                         Log.d("ParseXmlActivity", "In start tag = "+str1);
                         arrlat_long.add(str1);
                    }
                } 
                eventType = myxml.next();

            }
        } 
        catch (XmlPullParserException e) 
        {
        } 
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


   int in =Integer.parseInt(str);
       int in1 =Integer.parseInt(str1);   
       sampleGeoPoint = new GeoPoint(in,in1);
        return sampleGeoPoint;
    }}

1 个答案:

答案 0 :(得分:0)

我认为你的做法是错误的做法。我无法重写整个代码,但会给你一些提示,如何做到这一点。顺便说一下,它绝对没有任何关系,谷歌apis,它是你,谁在前台线程(又名UI线程)的onDraw方法中与webservices交谈!

为了摆脱这种情况,我建议如下: 绝对避免在onDraw(Canvas)内进行繁重的计算。使用AsyncTask来收集数据。在您提出请求后,您可以通过调用postInvalidate()来更新MapView并将其绘制。

也许overlaymanager lib的LazyLoad功能对你来说很有意思