Updating ViewFragment

Now, let's move on to ViewFragment. We will be using an Intent class to launch the built-in Google Maps app but with a pin to mark the location the photo was taken at.

The GPS location is already in the Photo object and we just need to do the following:

  • Capture the latitude and longitude in Double variables
  • Build a String from these values that will represent the URI that we need to create the Intent object
  • Create the Intent class
  • Start a Google Maps Activity passing in the Intent class

Of course, all this takes place inside the class that handles the button clicks for SHOW MAP.

Add the highlighted code that we just discussed to handle what happens when the user clicks on the SHOW MAP button:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

  View view = inflater.inflate(R.layout.fragment_view, container, false);
  TextView textView  = (TextView) view.findViewById(R.id.textView);
  Button buttonShowLocation = (Button) view.findViewById(R.id.buttonShowLocation);

  textView.setText(mCursor.getString(mCursor.getColumnIndex(DataManager.TABLE_ROW_TITLE)));

  mImageView = (ImageView) view.findViewById(R.id.imageView);
  mImageView.setImageURI(Uri.parse(mCursor.getString(mCursor.getColumnIndex(DataManager.TABLE_ROW_URI))));


  buttonShowLocation.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

      double latitude = Double.valueOf(mCursor.getString
  (mCursor.getColumnIndex(DataManager.
  TABLE_ROW_LOCATION_LAT)));
double longitude = Double.valueOf(mCursor.getString
  (mCursor.getColumnIndex(DataManager.
  TABLE_ROW_LOCATION_LONG)));

// Create a URI from the latitude and longitude
String uri = String.format(Locale.ENGLISH,
  "geo:%f,%f", latitude, longitude);

      // Create a Google maps intent
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));

      // Start the maps activity
      getActivity().startActivity(intent);
    }
  });


  return view;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.63.5