在我的应用程序中,我试图从存储在我的 sqllite 数据库中的坐标显示谷歌地图上多个点的纬度和经度。这是我的 Map.Class,我在其中显示点。
public class Map extends MapActivity {
private MapView map = null;
private MyLocationOverlay me = null;
List<Overlay> mapOverlays;
boolean flag;
public static Context context;
ArrayList<Article> mArticles;
EditText search;
DBHelper helper;
GeoPoint point;
MapView mapView;
Drawable marker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aproximite);
helper = DBHelper.getInstance(this);// <= data from mysqllitedatabase
map = (MapView) findViewById(R.id.mapview);
map.getController().setCenter(getPoint(40.76793169992044, -73.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
mapOverlays = map.getOverlays();
marker = getResources().getDrawable(R.drawable.icone_sur_carte);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker, map));//<===error occurance
me = new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}
@Override
public void onResume() {
super.onResume();
me.enableCompass();
}
public MapView getMapView() {
return this.map;
}
@Override
public void onPause() {
super.onPause();
me.disableCompass();
}
@Override
protected boolean isRouteDisplayed() {
return (false);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return (true);
} else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return (true);
}
return (super.onKeyDown(keyCode, event));
}
private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items = new ArrayList<OverlayItem>();
private Drawable marker = null;
private OverlayItem inDrag = null;
private ImageView dragImage = null;
private int xDragImageOffset = 0;
private int yDragImageOffset = 0;
private int xDragTouchOffset = 0;
private int yDragTouchOffset = 0;
private Context c;
public int selectedIndex = -1;
private final Bitmap bitMap;
private View view = null;
private boolean isPinch = false;
public SitesOverlay(Drawable marker, MapView mapView) {
super(boundCenter(marker));
this.marker = marker;
c = mapView.getContext();
dragImage = (ImageView) findViewById(R.id.drag);
xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2;
yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight();
mArticles = helper.getArticlesList();
for (int i=0;i<mArticles.size();i++){
double slat =Double.valueOf(mArticles.get(i).getLatitude().toString());
double vlong = Double.valueOf(mArticles.get(i).getLongitude().toString());
GeoPoint pt = new GeoPoint((int) (slat * 1E6), (int) (vlong * 1E6));
Log.e("lat long", "--- "+slat);
// Map.mc.animateTo(pt);
items.add(new OverlayItem(pt, mArticles.get(i).getNom().toString(), "Bendigo"));
boundCenter(marker);
}
bitMap = BitmapFactory.decodeResource(getResources(),
R.drawable.icone_sur_carte);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return (items.get(i));
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
public int size() {
return (items.size());
}
public void refresh() {
populate();
}
public void clear() {
items.clear();
resetLastFocuesIndex();
}
public void resetLastFocuesIndex() {
setLastFocusedIndex(-1);
selectedIndex = -1;
}
@Override
protected boolean onTap(final int index) {
if ( isPinch ){
return false;
}else{
/*
getMapView().setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (!items.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().invalidate();
}
}
getMapView().invalidate();
return true;
}
});*/
if (view != null) {
view.setVisibility(View.GONE);
getMapView().removeView(view);
getMapView().invalidate();
flag = false;
view = null;
}
view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.balloon_main_layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
ImageView image = (ImageView) view
.findViewById(R.id.balloon_disclosure);
TextView text = (TextView) view
.findViewById(R.id.balloon_item_title);
text.setText(items.get(index).getTitle());
if (items.get(index).getTitle() != null
&& items.get(index).getTitle().equals("Me") == false) {
image.setImageResource(R.drawable.icone_sur_carte);
}
Projection projection = getMapView().getProjection();
Point point = new Point();
projection.toPixels(items.get(index).getPoint(), point);
int x = (int) (view.getWidth() / 2f);
int y = -bitMap.getHeight() - 3;
MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
.getPoint(), x, y,
MapView.LayoutParams.BOTTOM_CENTER);
getMapView().removeView(view);
getMapView().invalidate();
getMapView().addView(view, lp);
getMapView().invalidate();
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (!items.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().invalidate();
System.out.println("SelectedIndex: "+selectedIndex);
}
}
getMapView().invalidate();
}
});
selectedIndex = index;
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
boolean result = false;
if (action == MotionEvent.ACTION_DOWN) {
for (OverlayItem item : items) {
Point p = new Point(0, 0);
map.getProjection().toPixels(item.getPoint(), p);
if (hitTest(item, marker, x - p.x, y - p.y)) {
result = true;
inDrag = item;
items.remove(inDrag);
populate();
xDragTouchOffset = 0;
yDragTouchOffset = 0;
setDragImagePosition(p.x, p.y);
dragImage.setVisibility(View.VISIBLE);
xDragTouchOffset = x - p.x;
yDragTouchOffset = y - p.y;
isPinch=false;
break;
}
}
} else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
if (view != null) {
if (view.getVisibility() != 0) {
Log.e("touch", " out move");
setDragImagePosition(x, y);
result = true;
isPinch=true;
}else{
setDragImagePosition(x, y);
isPinch=false;
}
}else{
setDragImagePosition(x, y);
result = true;
isPinch=true;
}
} else if (action == MotionEvent.ACTION_UP && inDrag != null) {
dragImage.setVisibility(View.GONE);
GeoPoint pt = map.getProjection().fromPixels(
x - xDragTouchOffset, y - yDragTouchOffset);
OverlayItem toDrop = new OverlayItem(pt, inDrag.getTitle(),
inDrag.getSnippet());
Log.e("touch", " out last" + pt.getLatitudeE6());
items.add(toDrop);
populate();
inDrag = null;
result = true;
isPinch = false;
}
return (isPinch || super.onTouchEvent(event,mapView));
}
private void setDragImagePosition(int x, int y) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage.getLayoutParams();
lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
- yDragImageOffset - yDragTouchOffset, 0, 0);
dragImage.setLayoutParams(lp);
}
}
}
当我运行此代码时,我的应用程序强制代码和我的日志显示以下错误。
05-25 14:18:38.125: E/AndroidRuntime(11240): FATAL EXCEPTION: main
05-25 14:18:38.125: E/AndroidRuntime(11240): java.lang.RuntimeException: Unable to start activity ComponentInfo{.TabSample}: java.lang.RuntimeException: Unable to start activity ComponentInfo{.map.Map}: java.lang.NumberFormatException
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.os.Looper.loop(Looper.java:130)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-25 14:18:38.125: E/AndroidRuntime(11240): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 14:18:38.125: E/AndroidRuntime(11240): at java.lang.reflect.Method.invoke(Method.java:507)
05-25 14:18:38.125: E/AndroidRuntime(11240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
05-25 14:18:38.125: E/AndroidRuntime(11240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-25 14:18:38.125: E/AndroidRuntime(11240): at dalvik.system.NativeStart.main(Native Method)
05-25 14:18:38.125: E/AndroidRuntime(11240): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{.map.Map}: java.lang.NumberFormatException
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1491)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:657)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.widget.TabHost.setCurrentTab(TabHost.java:329)
05-25 14:18:38.125: E/AndroidRuntime(11240): at .TabSample.setProximite(TabSample.java:659)
05-25 14:18:38.125: E/AndroidRuntime(11240): at .TabSample.onCreate(TabSample.java:98)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-25 14:18:38.125: E/AndroidRuntime(11240): ... 11 more
05-25 14:18:38.125: E/AndroidRuntime(11240): Caused by: java.lang.NumberFormatException
05-25 14:18:38.125: E/AndroidRuntime(11240): at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
05-25 14:18:38.125: E/AndroidRuntime(11240): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
05-25 14:18:38.125: E/AndroidRuntime(11240): at java.lang.Double.parseDouble(Double.java:318)
05-25 14:18:38.125: E/AndroidRuntime(11240): at java.lang.Double.valueOf(Double.java:356)
05-25 14:18:38.125: E/AndroidRuntime(11240): at .map.Map$SitesOverlay.<init>(Map.java:156)
05-25 14:18:38.125: E/AndroidRuntime(11240): at .map.Map.onCreate(Map.java:78)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 14:18:38.125: E/AndroidRuntime(11240): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-25 14:18:38.125: E/AndroidRuntime(11240): ... 20 more
05-25 14:23:44.253: I/Process(11240): Sending signal. PID: 11240 SIG: 9
我也想知道如何在 Map.Class 中集成后台服务