如何在android中单击按钮找到我的当前位置(纬度+经度)? [英] How to find my current location (latitude + longitude) on click of a button in android?

查看:129
本文介绍了如何在android中单击按钮找到我的当前位置(纬度+经度)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过各种代码片段,它们从诸如onlocationupdate()或onstatusChanged()之类的各种方法中找出坐标.我想要的是一个简单的代码,只需单击一下按钮即可获取当前的GPS坐标...

I have seen various code snippets which find outs co-ordinates from various methods like onlocationupdate() or onstatusChanged().... what i want is a simple code that fetches my current GPS co-ordinates on click of a button...

推荐答案

LocationManager mLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                LocationListener mLocListener = new MyLocationListener();
                mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);

public class MyLocationListener implements LocationListener{        

        public void onLocationChanged(Location loc) {           
            String message = String.format(
                        "New Location \n Longitude: %1$s \n Latitude: %2$s",
                        loc.getLongitude(), loc.getLatitude()
                );
                Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
        }
        public void onProviderDisabled(String arg0) {

        }
        public void onProviderEnabled(String provider) {

        }
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }       
    }

详细阅读 http://www.javacodegeeks .com/2010/09/android-location-based-services.html

这篇关于如何在android中单击按钮找到我的当前位置(纬度+经度)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆