如何在Android中获取地图上的纬度和经度?

发布于 2024-10-07 20:05:14 字数 43 浏览 0 评论 0原文

如何获取我在 Android 地图上长时间单击的特定位置的纬度和经度值?

How can I get the Latitude and Longitude values of a particular location that I have long clicked on the map in Android?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城旧梦 2024-10-14 20:05:14

对于长点击,我建议您查看 http://www. kind-kristiansen.no/2010/handling-longpresslongclick-in-mapactivity/。这将详细介绍如何在 Maps API 中侦听长点击事件,因为据我所知,内置功能很少或根本没有。

至于纬度/经度代码,长按后可以将像素转换为坐标。

public void recieveLongClick(MotionEvent ev)
{
    Projection p = mapView.getProjection();
    GeoPoint geoPoint = p.fromPixels((int) ev.getX(), (int) ev.getY());
    // You can now pull lat/lng from geoPoint
}

For the long click, I suggest you check out http://www.kind-kristiansen.no/2010/handling-longpresslongclick-in-mapactivity/. This will go into detail on how to listen for long click events within the Maps API since there is little or no built-in functionality that I know of.

As for the lat/lng code, after you get the long click you can translate the pixels to coordinates.

public void recieveLongClick(MotionEvent ev)
{
    Projection p = mapView.getProjection();
    GeoPoint geoPoint = p.fromPixels((int) ev.getX(), (int) ev.getY());
    // You can now pull lat/lng from geoPoint
}
帅气称霸 2024-10-14 20:05:14

您必须管理 LongClick 事件,然后使用以下代码来查找经度和纬度:

GeoPoint geoPoint=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
int latitude = geoPoint.getLatitudeE6();
int longitude = geoPoint.getLongitudeE6();

其中“event”是“MotionEvent”的对象。

根据您的情况使用任何其他事件。

You'll have to manage the LongClick event, and then use the code to find out longitude and latitude with the following code:

GeoPoint geoPoint=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
int latitude = geoPoint.getLatitudeE6();
int longitude = geoPoint.getLongitudeE6();

where 'event' is the object of 'MotionEvent'.

Use any other event according to your case.

猥琐帝 2024-10-14 20:05:14

它给出了地图点击点的纬度和经度

map.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {
            //myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));

              //The code below demonstrate how to convert between LatLng and Location

              //Convert LatLng to Location
              Location location = new Location("Test");
              location.setLatitude(point.latitude);
              location.setLongitude(point.longitude);
              location.setTime(new Date().getTime()); //Set time as current Date
              txtinfo.setText(location.toString());

              //Convert Location to LatLng
              LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());

              MarkerOptions markerOptions = new MarkerOptions()
                       .position(newLatLng)
                       .title(newLatLng.toString());

              map.addMarker(markerOptions);

        }
    });

It gives latitude and longitude on which point of map click

map.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {
            //myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));

              //The code below demonstrate how to convert between LatLng and Location

              //Convert LatLng to Location
              Location location = new Location("Test");
              location.setLatitude(point.latitude);
              location.setLongitude(point.longitude);
              location.setTime(new Date().getTime()); //Set time as current Date
              txtinfo.setText(location.toString());

              //Convert Location to LatLng
              LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());

              MarkerOptions markerOptions = new MarkerOptions()
                       .position(newLatLng)
                       .title(newLatLng.toString());

              map.addMarker(markerOptions);

        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文