android - 如何使用 Google map Intent 选择位置

标签 android google-maps android-intent google-maps-markers

我正在创建一个应用程序,用户可以在其中创建在校园内发生的事件。我希望有一种方法让用户可以通过按下按钮在 map 上选择一个位置。该按钮将打开一个谷歌 map Intent ,然后用户可以放置一个标记或将一个标记拖到他们想要的位置。一旦选定,坐标将作为一对 double 返回。然后我会将这些存储在一个数据库中,该数据库显示另一个 Activity 的点数。

我需要知道如何在 map Intent 上选择一个位置并返回选定的坐标。这是此 Activity 的代码。我已经删除了一些无关的日期选择方法等。

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 && 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();
    }
}

与此相关的所有其他问题都有非常过时的答案。谢谢你的帮助。

编辑
这是我目前用于 Intent 的代码:
 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);
            }
    }});

最佳答案

您可以实现 GoogleMap.OnMapLongClickListener 这将
允许用户在 map 上长按,它会给你
用户选择的 LatLng 对象或位置并添加您的自定义
那里的功能。
您可以找到文档 here

@Override
public void onMapLongClick(LatLng point) 
{
}

关于android - 如何使用 Google map Intent 选择位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29978098/

相关文章:

javascript - 信息窗口谷歌地图中的 Ionic 2 单击事件

android - 如果在内部类中没有抛出,则从 Intent 更改 View 内容

Android(3.0)操作栏不想去

java - 如何将文本逐行设置为多行编辑文本

android - 错误的第二个参数Android Studio(语音到文本)

javascript - 全屏谷歌地图

安卓:m​​yLocationOverlay.runOnFirstFix 太慢

android - 在不更改 Activity 类的情况下将回调传递给 startActivityForResult

java - 这是启动 Activity 的正确方法吗?

java - Android - 打开图库中的文件夹