第一次运行地图时没有出现标记



当我运行这个应用程序时,它会自动检测我的当前位置,但在我当前位置没有任何标记点,我可以知道为什么吗?其余的都很好,我能够pin点其他位置以及抓取地址回文本框。我面临的唯一问题是,当我第一次运行这个MAP时,标记没有出现在我当前的位置。为什么?

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
        txtLocation = (EditText) this.findViewById(R.id.txtLocation);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mc = mapView.getController();
        List<Overlay> mapOverlays = mapView.getOverlays();
        MapOverlay mapOverlay = new MapOverlay();
        mapOverlays.add(mapOverlay);
        // obtain gps location
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationListener = new MyLocationListener();
        lm.requestLocationUpdates(
        // LocationManager.GPS_PROVIDER,
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }
    private class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location loc) {
            if (loc != null) {
                // Toast.makeText(
                // getBaseContext(),
                // "Location changed: Lat: " + loc.getLatitude()
                // + " Lng: " + loc.getLongitude(),
                // Toast.LENGTH_SHORT).show();
            }
            p = new GeoPoint((int) (loc.getLatitude() * 1E6),
                    (int) (loc.getLongitude() * 1E6));
            mc.animateTo(p);
            mc.setZoom(18);
            // Add a location marker
            MapOverlay mapOverlay = new MapOverlay();
            List<Overlay> listofOverlays = mapView.getOverlays();
            listofOverlays.clear();
            listofOverlays.add(mapOverlay);
            // invalidate() method forces the MapView to be redrawn
            mapView.invalidate();
        }
        public void onProviderDisabled(String provider) {
        }
        public void onProviderEnabled(String provider) {
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    class MapOverlay extends com.google.android.maps.Overlay {
        @Override
        public boolean onTap(final GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub
            k = p;
            mc = mapView.getController();
            mc.animateTo(p);
            mapView.invalidate();

            new AlertDialog.Builder(ShowMaps.this)
                    .setTitle("Change location..")
                    .setMessage("Go to the new location?")
                    .setNegativeButton("NO",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    dialog.dismiss();
                                }
                            })
                    .setPositiveButton("YES",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    Geocoder geoCoder = new Geocoder(
                                            getBaseContext(), Locale
                                                    .getDefault());
                                    try {
                                        List<Address> addresses = geoCoder.getFromLocation(
                                                p.getLatitudeE6() / 1E6,
                                                p.getLongitudeE6() / 1E6, 1);
                                        String add = "";
                                        if (addresses.size() > 0) {
                                            for (int i = 0; i < addresses
                                                    .get(0)
                                                    .getMaxAddressLineIndex(); i++)
                                                add += addresses.get(0)
                                                        .getAddressLine(i);
                                        }
                                        Booking.txtLocation.setText(add);
                                        finish();
                                        // Toast.makeText(getBaseContext(), add,
                                        // Toast.LENGTH_SHORT).show();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }).show();
            return true;
        }
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
            super.draw(canvas, mapView, shadow);
            if (k != null) {
                // ---translate the GeoPoint to screen pixels---
                Point screenPts = new Point();
                mapView.getProjection().toPixels(k, screenPts);
                // ---add the marker---
                Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                        R.drawable.marker);
                canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
            }
            return true;
        }
    }
}

考虑做以下事情:

    mMyLocationOverlay = new MyLocationOverlay(this, mMapView);
    mMapView.getOverlays().add(mMyLocationOverlay);

你的代码添加看起来正确,也许你没有得到你的位置通过监听器,试图设置断点跟踪错误?

最新更新