android - 手动在谷歌地图上拖放图钉并相应地获取经度和纬度

标签 android google-maps latitude-longitude

在 Google map 中,我在用户的当前位置上放置了一个图钉,但现在我想为用户提供另一个功能,即他可以手动拖动该图钉并将其放置在 map 上的任何位置,也可以在他手动放置图钉时使用我可以收到一个回调,它为我提供了用户放下该图钉的那个位置的经度和纬度。

如果你能给我一些例子,我将非常感激。

最佳答案

在过去的几天里,我终于找到了如何实现你描述的那些功能,因为这个问题没有答案,我决定发布我发现的东西,尽管你已经问了很长时间了,你可能会发现另一个自己的答案。

所以,我使用了 this 的代码混帐。我设法做了你想做的所有事情,除了我没有以那种方式尝试过的回调。按下按钮后,我得到了图钉的位置。


没有必要发布我的 AndroidManifest,因为我读到您在显示您的位置方面没有问题,所以所需的权限没问题。


唯一需要做的就是创建一个名为 MapsActivity 的 Activity 。 这是它的全部内容:

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import common.logger.Log;

public class MapsActivity extends Activity implements
        OnMapReadyCallback, GoogleMap.OnInfoWindowClickListener,
        GoogleMap.OnMarkerDragListener {

    LatLng position = new LatLng(34.6767, 33.04455);
    final Marker marker_final = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.
                        map);
        mapFragment.getMapAsync(this);

        ImageButton returnback = (ImageButton)findViewById(R.id.returnback);
        returnback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Toast.makeText(MapsActivity.this.getApplicationContext(), position.latitude + ":"  + position.longitude, Toast.LENGTH_LONG).show();
            }
        });


    }

    @Override
    public void onMapReady(GoogleMap map) {


        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 13));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
        map.animateCamera(zoom);

        map.addMarker(new MarkerOptions()
                .title("Shop")
                .snippet("Is this the right location?")
                .position(position))
                .setDraggable(true);

       // map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
        map.setOnInfoWindowClickListener(this);
        map.setOnMarkerDragListener(this);
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onMarkerDragStart(Marker marker) {
        LatLng position0 = marker.getPosition();

        Log.d(getClass().getSimpleName(), String.format("Drag from %f:%f",
                position0.latitude,
                position0.longitude));
    }

    @Override
    public void onMarkerDrag(Marker marker) {
        LatLng position0 = marker.getPosition();

        Log.d(getClass().getSimpleName(),
                String.format("Dragging to %f:%f", position0.latitude,
                        position0.longitude));

    }

    @Override
    public void onMarkerDragEnd(Marker marker) {
        position = marker.getPosition();

        Log.d(getClass().getSimpleName(), String.format("Dragged to %f:%f",
                position.latitude,
                position.longitude));
    }
}

这是我的 activity_maps.xml 布局(我还添加了一个编辑文本,但没有必要使用它):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:background="#ecf0f1">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView3" >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:weightSum="1"
                android:layout_height="wrap_content">

                <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.9"
                    android:id="@+id/locationsearch" />

                <ImageButton
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:id="@+id/search"
                    android:layout_weight="0.1"
                    android:background="@drawable/search" />

            </LinearLayout>

            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="405dp"
            android:name="com.google.android.gms.maps.MapFragment"/>

            <ImageButton
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:id="@+id/returnback"
                android:background="@drawable/less" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

关于android - 手动在谷歌地图上拖放图钉并相应地获取经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14208952/

相关文章:

android - 在 Phonegap Android 应用程序中忽略了视口(viewport)设置,但仅在某些手机上?

java - 无法在 com.google.gms.googleservices.GoogleServicesPlugin 的空对象上调用方法 get()

javascript - 在浏览器中打开带有特定地址的谷歌地图

javascript - 谷歌地图全高风格angularjs

iphone - 在 iPhone 上将经度/纬度转换为 x/y

java - Android 应用程序在解析一个特定网站时崩溃

javascript - 如何在 Google map 中添加新圈子之前自动删除先前的圈子

android - 印度的 LatLngBounds 是什么?

php - 使用经度/纬度计算 OSGR

android - 如何在线性布局中一起滚动 2 个 GridView?