title: How to use a Google Maps intent to select a location
body: <p>I am creating an app in which a user can create events that are happening on campus. I would like to have a way for a user to select a location on a map on a button push. The button would open an Google Maps intent which the user could then place a marker or drag one to their desired location. Once selected the coordinates would return as a pair of doubles. I will be then storing these in a database which displays the points on another activity.</p>
  
  <p>I need to know how to select a location on a map intent and return the selected coordinates. Here is the code for this activity. I have removed some of the extraneous methods for date picking and such.</p>
  
  <pre><code>public class CreateEvent extends Activity {
  
      EditText startTimeField;
      EditText endTimeField;
      EditText startDateField;
      EditText endDateField;
      EditText locationField;
      Calendar startTime;
      Calendar endTime;
      ImageView mImageView;
      Bitmap imageBitmap;
      Button capture;
      Button locationButton;
      static final int REQUEST_IMAGE_CAPTURE = 1;
  
      SimpleDateFormat time_format = new SimpleDateFormat("hh:mm a", Locale.US);
      SimpleDateFormat date_format = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_create_event);
  
          startTime = new GregorianCalendar();
          endTime = new GregorianCalendar();
          endTime.set(Calendar.HOUR, endTime.get(Calendar.HOUR) + 1);
  
          startTimeField = (EditText)this.findViewById(R.id.startTimeField);
          endTimeField = (EditText)this.findViewById(R.id.endTimeField);
          startDateField = (EditText)this.findViewById(R.id.startDateField);
          endDateField = (EditText)this.findViewById(R.id.endDateField);
          locationField = (EditText) this.findViewById(R.id.locationField);
          startTimeField.setKeyListener(null);
          endTimeField.setKeyListener(null);
          startDateField.setKeyListener(null);
          endDateField.setKeyListener(null);
          startTimeField.setText(time_format.format(startTime.getTime()));
          endTimeField.setText(time_format.format(endTime.getTime()));
          startDateField.setText(date_format.format(startTime.getTime()));
          endDateField.setText(date_format.format(endTime.getTime()));
  
          mImageView = (ImageView) this.findViewById(R.id.eventPhoto);
  
  
          if(savedInstanceState != null){
              imageBitmap = savedInstanceState.getParcelable("eventPhoto");
              if(imageBitmap != null){
                  mImageView.setImageBitmap(imageBitmap);
              }
          }
  
          capture = (Button) findViewById(R.id.photoButton);
          capture.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                  startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
              }
          });
  
          locationButton = (Button) findViewById(R.id.locationButton);
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == REQUEST_IMAGE_CAPTURE &amp;&amp; resultCode == RESULT_OK) {
              Bundle extras = data.getExtras();
              imageBitmap = (Bitmap) extras.get("data");
              mImageView.setImageBitmap(imageBitmap);
          }
      }
  
      @Override
      public void onSaveInstanceState(Bundle outstate){
          outstate.putParcelable("eventPhoto", imageBitmap);
          super.onSaveInstanceState(outstate);
  
      public void makeEvent(View view){
          Intent intent = new Intent();
          intent.putExtra("Event", new Event(imageBitmap, ((EditText)findViewById(R.id.eventField)).getText().toString(),
                  ((EditText)findViewById(R.id.locationField)).getText().toString(), startTime.getTime(),
                  endTime.getTime(), ((EditText)findViewById(R.id.descriptionField)).getText().toString()));
  //        intent.putExtra("Bitmap", );
  //        intent.putExtra("Name", );
  //        intent.putExtra("Location", );
  //        intent.putExtra("Start", );
  //        intent.putExtra("End", );
  //        intent.putExtra("Description", );
          setResult(RESULT_OK, intent);
          finish();
      }
  }
  </code></pre>
  
  <p>All other questions regarding this have very outdated answers. Thanks for any help.</p>
  
  <p><strong>EDIT</strong>
  Here is the code I am currently using for the intent:</p>
  
  <pre><code> locationButton = (Button) findViewById(R.id.locationButton);
      locationButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              Uri gmmIntentUri = Uri.parse("geo:42.273856,-71.805976?z=16");
              Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
              mapIntent.setPackage("com.google.android.apps.maps");
              if (mapIntent.resolveActivity(getPackageManager()) != null) {
                  startActivity(mapIntent);
              }
      }});
  </code></pre>