在android上绘制地图叠加

时间:2011-11-24 19:52:53

标签: java android google-maps nullpointerexception google-maps-markers

public void draw(Canvas canvas, MapView mapView, boolean shadow) {    

// Get the map projection to convert lat/long to screen coordinates
Projection projection = mapView.getProjection();

Point lPoint = new Point();
//  projection.toPixels(locationPoint, lPoint);

// Draw the overlay
if (shadow == false) {
  if (friendLocations.size() > 0) {
    Iterator<String> e = friendLocations.keySet().iterator();
    do {
      // Get the name and location of each contact
      String name = e.next();          
      Location location = friendLocations.get(name);
      if(location!=null)
     {
      // Convert the lat / long to a Geopoint
      Double latitude = location.getLatitude()*1E6;
      Double longitude = location.getLongitude()*1E6;
      //GeoPoint geopoint = new GeoPoint(latitude.intValue(),longitude.intValue());
      GeoPoint geopoint = new GeoPoint(19240000,-99120000); 
      // Ensure each contact is within 10km
      float dist = location.distanceTo(getLocation()); 
      if (dist < 10000) {
      Point point = new Point();
       projection.toPixels(geopoint, point);

        // Draw a line connecting the contact to your current location.
        canvas.drawLine(lPoint.x, lPoint.y, point.x, point.y, paint);

        // Draw a marker at the contact's location.
        RectF oval = new RectF(point.x-markerRadius,
                               point.y-markerRadius,
                               point.x+markerRadius,
                               point.y+markerRadius);

        canvas.drawOval(oval, backPaint);
        oval.inset(2, 2);
        canvas.drawOval(oval, paint);

        // Draw the contact's name next to their position.
        float textWidth = paint.measureText(name);
        float textHeight = paint.getTextSize();
        RectF textRect = new RectF(point.x+markerRadius, point.y-textHeight,
                                   point.x+markerRadius+8+textWidth, point.y+4);
        canvas.drawRoundRect(textRect, 3, 3, backPaint);
        canvas.drawText(name, point.x+markerRadius+4, point.y, paint);
      }
     }
    } while (e.hasNext());
  }
}
super.draw(canvas, mapView, shadow);
}

有谁能告诉我上面的代码片段有什么问题 logcat在函数java.lang.Nullpointer

处显示distanceTo异常

我尝试了所有替代方案,例如为位置对象提供初始坐标并直接初始化geopoint似乎没有任何效果。

0 个答案:

没有答案