安卓 : how to capture a image of GPS location

标签 android google-maps gps

我正在使用 Google map 查找所提供地址的位置。

我想存储从 Google map onClick 获取的位置图像。

有人知道怎么做吗?

这是我的谷歌地图和存储 onClick 的代码

public class TestGPSActivity extends MapActivity 
 {    
MapView mapView;
private MapController mc;
private GeoPoint p;
private double lng;
private double lat;
private Address address;
private View mCurrentUrlMask;
private File imageFile; 

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<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) + "\n";
            }

            Toast.makeText(getBaseContext(), add, Toast.LENGTH_LONG).show();
        }
        catch (IOException e) {                
            e.printStackTrace();
        }   
        return true;
    }
    else                
        return false;
}             


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps_main);

    mapView = (MapView) findViewById(R.id.mapView); 

    mapView.setDrawingCacheEnabled(true);


     // image naming and path  to include sd card  appending name you choose for file
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + GPSActivity.DIRECTORY;   

    // create bitmap screen capture
    Bitmap bitmap;

    View v1 = mapView.getRootView();

    v1.setDrawingCacheEnabled(true);

    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    OutputStream fout = null;
    imageFile = new File(DemoCamGPSActivity.DIRECTORY);

    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
        fout.flush();
        fout.close();

    } catch (FileNotFoundException e) {
        System.out.println("in file not found");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
    View zoomView = mapView.getZoomControls();


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

    mc = mapView.getController();

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = null;
    try {   

        addresses = geocoder.getFromLocationName(getIntent().getStringExtra("address"), 1);

    } catch (IOException e) {



    }

    try{ 
        address = addresses.get(0);  
    }
    catch (Exception e) {
        flash("Unable to locate given address");
        startActivity(new Intent(this, GPSMainActivity.class));
        finish();
    }
    try{
      lng  = address.getLongitude();
      lat  = address.getLatitude();   

      GPSActivity.writeLog(getIntent().getStringExtra("address") +" GPS co-ordibnates : "+lat+" , "+lng);
    }catch (Exception e) {


    }


    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();
    mapView.invalidate();        
}
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);
}    
 }

我收到这个错误

03-28 16:54:12.580: E/AndroidRuntime(30010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mentha.Demo/com.mentha.Demo.TestGPSActivity}: java.lang.NullPointerException

我在这个特定代码上遇到错误

   // create bitmap screen capture
    Bitmap bitmap;

    View v1 = mapView.getRootView();

    v1.setDrawingCacheEnabled(true);

    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    OutputStream fout = null;
    imageFile = new File(DemoCamGPSActivity.DIRECTORY);

    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
        fout.flush();
        fout.close();

    } catch (FileNotFoundException e) {
        System.out.println("in file not found");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    Uri uri = Uri.fromFile(new File(mPath));

请帮帮我..提前致谢

最佳答案

使用静态 map 会是您正在寻找的解决方案吗?

https://developers.google.com/maps/documentation/staticmaps/

关于安卓 : how to capture a image of GPS location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9905250/

相关文章:

android - 如何在 Android 中将两个按钮放在同一行

android - Android Studio编译Gradle错误

android - 如何隐藏虚拟键盘

google-maps - 显示/隐藏 Gmap 显示时显示部分灰色

javascript - 如何在没有收到 OVER_QUERY_LIMIT 响应的情况下对 20 个地址进行地理编码?

ios - 静态库中的 Google Maps iOS SDK

ios - CoreLocation CLLocationManager requestLocation 需要10秒才能得到回调

gps - 如何获取谷歌地图路线起点和终点之间的所有 GPS 坐标?

android - Android 是否支持慢动作视频播放?

gps - xamarin 跨平台获取位置