Android : how to capture a image of GPS location

后端 未结 3 870
后悔当初
后悔当初 2020-11-27 07:34

I am using Google Maps for finding the location on a provided address.

I want to store the image of location which I get from Google Maps onClick.

3条回答
  •  庸人自扰
    2020-11-27 08:10

    I have solved this problem. I was having problem with image capturing of GPS location. I have just created the capture menu, In which after uploading the GPS location one can capture the image by clicking on capture menu button.

    Here is the whole code for capturing the screenshot of GPS location & how to find GPS location.

    public class TestGPSActivity extends MapActivity 
    {    
    MapView mapView;
    private MapController mc;
    private GeoPoint p;
    private double lng;
    private double lat;
    private Address address; 
    
    //google Map
    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.pin);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }
    } 
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
        //---when user lifts his finger---
        if (event.getAction() == 1) {                
            GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(),
                    (int) event.getY());
    
            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
            try {
                List
    addresses = geoCoder.getFromLocation( p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1); String add = ""; if (addresses.size() > 0) { for (int i=0; i addresses = null; try { addresses = geocoder.getFromLocationName(getIntent().getStringExtra("address"), 1); } catch (IOException e) { e.printStackTrace(); } try{ address = addresses.get(0); } catch (Exception e) { flash("Unable to locate given address"); startActivity(new Intent(this, GPSMainActivity.class)); finish(); } try{ //stores the Longitude n lattitude in log file lng = address.getLongitude(); lat = address.getLatitude(); //for writing into log.text HomeActivity.writeLog(getIntent().getStringExtra("address") +" GPS co-ordibnates : "+lat+" , "+lng); }catch (Exception e) { e.printStackTrace(); } p = new GeoPoint( (int) (lat * 1E6), (int) (lng * 1E6)); mc.animateTo(p); mc.setZoom(17); //---Add a location marker--- MapOverlay mapOverlay = new MapOverlay(); List listOfOverlays = mapView.getOverlays(); listOfOverlays.clear(); listOfOverlays.add(mapOverlay); mapView.invalidate(); //mapView.invalidate(); } //ScreenShot of Image private void screenshot() { try{ String mapDIR = HomeActivity.DIRECTORY+"/Image"; File f = new File(mapDIR); if(! f.exists()) { f.mkdir(); } MapView v1 = (MapView)findViewById(R.id.mapView); v1.setDrawingCacheEnabled(true); Bitmap b = Bitmap.createBitmap(v1.getDrawingCache()); try { FileOutputStream fos = new FileOutputStream(mapDIR+"/"+System.currentTimeMillis()+".png"); b.compress(CompressFormat.PNG, 50, fos); fos.close(); } catch (FileNotFoundException e) { Toast.makeText(getApplicationContext(), e.getMessage(),Toast.LENGTH_LONG).show(); e.printStackTrace(); } catch (IOException e) { Toast.makeText(getApplicationContext(), e.getMessage(),Toast.LENGTH_LONG).show(); e.printStackTrace(); } v1.setDrawingCacheEnabled(false); // clear drawing cache }catch (Exception e) { Toast.makeText(getApplicationContext(), e.getMessage(),Toast.LENGTH_LONG).show(); e.printStackTrace(); } } private void flash(String data) { Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show(); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK) { startActivity(new Intent(this, GPSMainActivity.class)); finish(); } return super.onKeyDown(keyCode, event); } //menu button @Override public boolean onCreateOptionsMenu(android.view.Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, 1, 0 , "Exit"); menu.add(0, 2, 0 , "Capture Image"); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { switch(item.getItemId()) { case 1: startActivity(new Intent(this, GPSMainActivity.class)); finish(); break; case 2: screenshot(); Toast.makeText(getApplicationContext(), "Captured Map", Toast.LENGTH_SHORT).show(); break; } return super.onMenuItemSelected(featureId, item); } }

    Thanks for All.. Hope this will help some one..

提交回复
热议问题