android - 在 map 中定位我的位置的问题

标签 android maps location

我在 map View 的帮助下制作了一张 map 。我的 map 可以正常工作,它还允许我制作 maap 的屏幕截图。但我无法找到我当前的位置。我不知道为什么我的代码显示在这里。谁能告诉我如何找到我当前的位置。

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

        locationListener = new GPSLocationListener();


        locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 
            0, 
            0, 
            locationListener);

//        Location intial = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//        GeoPoint mainone=new GeoPoint((int) (intial.getLatitude() * 1E6), 
//                (int) (intial.getLongitude() * 1E6));
//        
//        mapController.animateTo(mainone);

        mapView = (MapView) findViewById(R.id.mapView);
        capture=(Button) findViewById(R.id.bcapture);

        capture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            getMapImage();
            saveMapImage();

            }
        });

        // enable Street view by default
        mapView.setStreetView(false);

        // enable to show Satellite view
        // mapView.setSatellite(true);

        // enable to show Traffic on map
        // mapView.setTraffic(true);
        mapView.setBuiltInZoomControls(true);

        mapController = mapView.getController();
//        mapController.setZoom(16); 
    }
    private Bitmap getMapImage() {  
        /* Position map for output */  
        MapController mc = mapView.getController();  
//        mc.setCenter(SOME_POINT);  
//        mc.setZoom(16);  

        /* Capture drawing cache as bitmap */  
        mapView.setDrawingCacheEnabled(true);  
        Bitmap bmp = Bitmap.createBitmap(mapView.getDrawingCache());  
        mapView.setDrawingCacheEnabled(false);  

        return bmp;  
    }  

    private void saveMapImage() {  
        String filename = "foo.png";  
        File f = new File("/sdcard/", filename);  
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(f);
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }  

        Bitmap bmp = getMapImage();  

        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);  

        try {
            out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }  
    }  
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    private class GPSLocationListener implements LocationListener 
    {

        @Override
        public void onLocationChanged(Location location) {
            Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location1 != null) {

                GeoPoint point = new GeoPoint(
                        (int) (location1.getLatitude() * 1E6), 
                        (int) (location1.getLongitude() * 1E6));

                /* Toast.makeText(getBaseContext(), 
                        "Latitude: " + location.getLatitude() + 
                        " Longitude: " + location.getLongitude(), 
                        Toast.LENGTH_SHORT).show();*/
                   System.out.println(point.getLatitudeE6()+point.getLongitudeE6()+"helooooooo");
                   System.out.println(point.getLatitudeE6()+"buhahahooo");
                mapController.animateTo(point);
//                mapController.setZoom(16);

                // add marker
                MapOverlay mapOverlay = new MapOverlay();
                mapOverlay.setPointToDraw(point);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);

                String address = ConvertPointToLocation(point);
                Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

                mapView.invalidate();
            }
        }

        public String ConvertPointToLocation(GeoPoint point) {   
            String address = "";
            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(
                    point.getLatitudeE6()  / 1E6, 
                    point.getLongitudeE6() / 1E6, 1);

                if (addresses.size() > 0) {
                    for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                        address += addresses.get(0).getAddressLine(index) + " ";
                }
            }
            catch (IOException e) {                
                e.printStackTrace();
            }   

            return address;
        } 

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    class MapOverlay extends Overlay
    {
        private GeoPoint pointToDraw;

        public void setPointToDraw(GeoPoint point) {
            pointToDraw = point;
        }

        public GeoPoint getPointToDraw(
                ) {
            return pointToDraw;
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);                   

            // convert point to pixels
            Point screenPts = new Point();
            mapView.getProjection().toPixels(pointToDraw, screenPts);

            // add marker
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image        
            return true;
        }
    } 

最佳答案

尝试使用 MyLocationOverlay 类的对象

MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this,mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverLay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable(){
   public void run(){
      mapView.getController().animateTo(myLocationOverlay.getMyLocation());
   }
});

这会解决你的问题

关于android - 在 map 中定位我的位置的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8950840/

相关文章:

android - 最后的json值显示在android中的expandablelistview中

android - 检索 StatusbarNotification 详细信息(标题、通知文本)的可靠方法

android - 谷歌 API key 丢失

c++ - C++的多重关联问题

java - 如何通过Java类在andoridmanifest.xml中添加权限?

ios - 如何使用PHPhotoKit将CLLocation的GPS信息写入UIImage?

java - Android 应用程序 - 共享首选项未正确加载

javascript - 从 HTML 输入中检索多个值并存储在 JSON 键、值 [Array] 中

algorithm - 使用 GPS/Compass/Accelerometer - 算法到达航路点?

java - 我需要解释 Android 的 JNI 代码行