2012-03-28 41 views
3

我正在使用Google地圖查找提供的地址上的位置。Android:如何捕捉GPS位置的圖像

我想存儲從Google Maps 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 

我對這個perticular碼得到錯誤

// 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)); 

請幫忙碼我出來..提前致謝

+1

取當前視圖 – 2012-03-28 10:24:26

+0

截圖@imrankhan我試圖採取特定的看法,但無法截圖..請查看我的代碼..我用代碼更新了問題..提前謝謝 – 2012-03-28 11:32:54

+0

NullPointer在哪一行? – Ferdau 2012-03-28 11:50:38

回答

2

我已經解決了這個問題。我在GPS位置的圖像捕捉方面存在問題。我剛剛創建了捕捉菜單,在上傳GPS位置後,可以通過單擊捕捉菜單按鈕來捕捉圖像。

這裏是捕獲GPS位置截圖的整個代碼&如何找到GPS位置。

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<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); 
    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) { 
     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<Overlay> 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); 
} 
} 

感謝所有..希望這將幫助一些一..

+0

通過這個博客 - http://findgpslocation.blogspot。在/你會得到有關如何找到GPS位置的信息。 – 2012-09-24 04:55:05

0

我認爲this答案會幫助你

+1

shereef - 謝謝我會在確認後回覆您。 – 2012-03-28 10:33:32

+0

PLZ查看我的代碼..謝謝 – 2012-03-28 11:38:49

3

可以使用靜態地圖是您正在尋找的解決方案嗎?

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

+0

它給出了谷歌地圖的所有細節..我想知道我可以如何在我的代碼中實現.. ??我應該如何以JPEG或PNG格式存儲圖像..?請幫助我..謝謝 – 2012-03-28 11:07:06

+0

upadted我的代碼.. plz退房。謝謝 – 2012-03-28 11:39:15

+0

只要使用這個網址:http://maps.googleapis.com/maps/api/staticmap?center=ghent&size=512x512&maptype=roadmap&sensor=false求變「的根特」與地址或座標試試,剛讀鏈接我給你正確的...你可以添加標記和東西...只是谷歌如何保存從一個網址的圖像。 – Ferdau 2012-03-28 11:55:59