2014-11-22 89 views
0

我已經在google地圖中實現了一個longclick listener。所以當用戶長時間點擊地圖時,它會啓動相機意圖,然後您可以拍攝照片。現在我想要實現的是當圖像被放置在用戶長時間點擊的地圖上的點上時。在谷歌地圖API添加cutom標記v2

googleMap.setOnMapLongClickListener(Test.this); 
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

// adding marker 

googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
googleMap.setMyLocationEnabled(true); // false to disable 
googleMap.getUiSettings().setZoomControlsEnabled(false); // true to enable 
googleMap.getUiSettings().setCompassEnabled(true); 
googleMap.getUiSettings().setMyLocationButtonEnabled(true); 

    } 
    { 
} 

    @Override 
    public void onMapLongClick(LatLng point) { 
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
    startActivityForResult(intent,TAKE_PICTURE); 
    googleMap.addMarker(new MarkerOptions().position(point) 
      .icon(BitmapDescriptorFactory.fromResource(TAKE_PICTURE))); 

    Toast.makeText(getApplicationContext(), 
     "New marker [email protected]" + point.toString(), Toast.LENGTH_LONG) 
     .show(); 
    } 
    } 

所以基本上現在應用程序崩潰,奇怪的是,我似乎不能儘快檢查錯誤消息(logcat的)cuase,因爲它涉及了一遍自敗。 (我已經嘗試過打印屏幕,但速度不夠快:-))

任何人都可以請擺脫這一點,我可以做些什麼來解決這個問題?

感謝

+0

Eclipse和Android Studio中有特殊的按鈕來停止logcat窗口的自動滾動。您也可以從命令行「adb logcat -d> log.txt」執行以獲取日誌文件 – Mixaz 2014-11-22 18:38:13

回答

0

這看起來錯了我:

startActivityForResult(intent,TAKE_PICTURE); 
googleMap.addMarker(new MarkerOptions().position(point) 
     .icon(BitmapDescriptorFactory.fromResource(TAKE_PICTURE))); 

的第一條語句使用TAKE_PICTURE作爲啓動活動的請求的代碼。這可能是對的。第二條語句使用TAKE_PICTURE作爲資源標識符(對'res'文件夾中的某些內容)。很有可能第二個語句會拋出一個異常,因爲該標識符沒有資源,或者它對於BitmapDescriptorFactory而言類型錯誤。

當您撥打startActivityForResult時,相機應用程序啓動,用戶進行拍攝需要一段時間(或通過BACK按鈕取消相機應用程序)。你不能訪問立即捕捉到的照片,你需要做的是,在onActivityResult處理程序,即:

private File photo = null; 
/** 
* This method is used to start the camera activity and save the image taken as the imagename passed 
* 
* @param imagename : this is the name of the image which will be saved 
*/ 
private void clickPicture(String imagename) { 
    Intent getCameraImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // this is the same as ""android.media.action.IMAGE_CAPTURE" 
    File cameraFolder; 
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
     cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"myfolder/"); 
    else 
     cameraFolder= context.getCacheDir(); 
    if(!cameraFolder.exists()) 
     cameraFolder.mkdirs(); 
    String imageFileName = imagename; 
    photo = new File(cameraFolder, "myfolder/" + imageFileName); 
    getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 
    Uri.fromFile(photo); 
    startActivityForResult(getCameraImage, TAKE_PICTURE); 

}  

protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    if(resultCode == RESULT_OK) { 
     // read captured image from File photo and place it to map 
    } 
} 

由於圖像會很大的一個標誌,你應該把它重新取樣,以更小的東西。這是其他問題,谷歌它))

圖像文件將被保存在應用程序的外部存儲(在SD卡上),如果它被安裝。否則,該文件將被放置到內部存儲器中的應用程序緩存目錄。

+0

非常感謝!讓它在沒有崩潰的情況下工作,但似乎無法將圖像放回到地圖中。 (不要擔心調整大小,只是想讓它在地圖中放置圖像)我會用更新後的代碼提出一個新問題。 – wemapps 2014-11-23 07:06:21

+1

嗨,我問這裏的另一個問題是鏈接[http://stackoverflow.com/questions/27086649/how-to-place-taken-photo-from-camera-intent-and-place-in-maps-where-用戶 - 擁有-TA] – wemapps 2014-11-23 07:40:04