模拟器只显示一个标记

发布于 2024-12-05 02:51:40 字数 3452 浏览 1 评论 0原文

我试图在每次用户触摸屏幕时放置一个新标记。 以下是我的代码,我的问题是新标记根本不出现

package com.org;

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

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.MapView.LayoutParams;
import com.google.android.maps.Overlay;

public class MapExampleActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
MapOverlay mapOverlay = new MapOverlay();
private MyLocationOverlay myLocOverlay;

class MapOverlay extends com.google.android.maps.Overlay {
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        super.draw(canvas, mapView, shadow);

        // ---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        // ---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.marker);
        canvas.drawBitmap(bmp, screenPts.x + 25, screenPts.y - 50,null);
        return true;
    }
}


@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


            mapView = (MapView) findViewById(R.id.mymap);
     LinearLayout zoomLayout = (LinearLayout)findViewById  (R.id.myzoom);  
            View zoomView = mapView.getZoomControls(); 

            zoomLayout.addView(zoomView, 
                new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT)); 
            mapView.displayZoomControls(true);

            mc = mapView.getController();
            String coordinates[] = {"1.352566007", "103.78921587"};
            double lat = Double.parseDouble(coordinates[0]);
            double lng = Double.parseDouble(coordinates[1]);

            p = new GeoPoint(
                (int) (lat * 1E6), 
                (int) (lng * 1E6));

            mc.animateTo(p);
            mc.setZoom(17); 


    // ---Add a location marker---
    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);

    mapView.invalidate();

}


@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
public boolean onTouchEvent(MotionEvent event, MapView mapView) 
     {   
     //---when user lifts his finger---
     if (event.getAction() == 1) {                
          GeoPoint p1 = mapView.getProjection().fromPixels(
             (int) event.getX(),
             (int) event.getY());


                 mc.animateTo(p1);
                 mc.setCenter(p1);
                 return true;
     }                            
     else 
         return false;
  }        


  }

i am trying to put a new marker everytime the user touches the screen.
the following is my code, my problem is that the new marker doesn't appear at all

package com.org;

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

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.MapView.LayoutParams;
import com.google.android.maps.Overlay;

public class MapExampleActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
MapOverlay mapOverlay = new MapOverlay();
private MyLocationOverlay myLocOverlay;

class MapOverlay extends com.google.android.maps.Overlay {
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        super.draw(canvas, mapView, shadow);

        // ---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        // ---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.marker);
        canvas.drawBitmap(bmp, screenPts.x + 25, screenPts.y - 50,null);
        return true;
    }
}


@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


            mapView = (MapView) findViewById(R.id.mymap);
     LinearLayout zoomLayout = (LinearLayout)findViewById  (R.id.myzoom);  
            View zoomView = mapView.getZoomControls(); 

            zoomLayout.addView(zoomView, 
                new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT)); 
            mapView.displayZoomControls(true);

            mc = mapView.getController();
            String coordinates[] = {"1.352566007", "103.78921587"};
            double lat = Double.parseDouble(coordinates[0]);
            double lng = Double.parseDouble(coordinates[1]);

            p = new GeoPoint(
                (int) (lat * 1E6), 
                (int) (lng * 1E6));

            mc.animateTo(p);
            mc.setZoom(17); 


    // ---Add a location marker---
    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);

    mapView.invalidate();

}


@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
public boolean onTouchEvent(MotionEvent event, MapView mapView) 
     {   
     //---when user lifts his finger---
     if (event.getAction() == 1) {                
          GeoPoint p1 = mapView.getProjection().fromPixels(
             (int) event.getX(),
             (int) event.getY());


                 mc.animateTo(p1);
                 mc.setCenter(p1);
                 return true;
     }                            
     else 
         return false;
  }        


  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2024-12-12 02:51:40

试试这个。将您的覆盖层 onDraw() 方法替换为以下方法:

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);    
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
for(int i=0;i<markerList.size();i++) {
    canvas.drawBitmapbmp(markerList.get(i), screenPts.x + 25, screenPts.y - 50,null);
}
   return false;
}

并将此方法添加到您的覆盖层类中,并在覆盖层类的顶部创建一个标记列表。

public boolean onTap(GeoPoint p, MapView map) {

Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);

// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.marker);
markerList.add(bmp);
return true;            
}

Try this one. Replace your overlay onDraw() method with this one:

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);    
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
for(int i=0;i<markerList.size();i++) {
    canvas.drawBitmapbmp(markerList.get(i), screenPts.x + 25, screenPts.y - 50,null);
}
   return false;
}

and add this method to your overlay class and also create a markerList at the top of your overlay class.

public boolean onTap(GeoPoint p, MapView map) {

Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);

// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.marker);
markerList.add(bmp);
return true;            
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文