【问题标题】:How to current location in Android using google Maps?如何使用谷歌地图在 Android 中定位当前位置?
【发布时间】:2014-05-27 10:06:21
【问题描述】:

我正在尝试通过显示他在哪个区域来获取用户的位置,在 Android API 的帮助下在谷歌地图中的特定位置,同时在互联网上冲浪我开始知道 getLocationManger() 用于查看位置但它仅按纬度、经度显示。

如果有人对此有任何想法,请帮助我的朋友。

【问题讨论】:

  • 好的,先生,让我试试这个,然后就知道了
  • 但是先生,我尝试了 androidhive 教程来获取当前位置,它没有显示地图,我们无法查看我们实际上是在哪里和哪个位置单击按钮,一个 toastmessage 显示为纬度,经度。
  • 是的,现在你得到了 lat 和 long 值......只需将它们存储在两个双精度值中(gps.getlatiue(),gps.getlongitude)并将它们放在你的谷歌地图中。
  • 好的先生,我会继续这样做,如果还有任何疑问,请指导我。

标签: android google-maps geolocation


【解决方案1】:

您可以根据纬度和经度获取用户的当前位置,并简单地使用这些值创建一个隐式意图来启动地图。

获取当前位置:

LocationManager locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
            LocationListener locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {

                        //Toast.makeText(getApplicationContext(),"Entered onLocationChanged",Toast.LENGTH_SHORT).show();
                        Log.d("LIFECYCLE","Entered onLocationChanged");
                        lat = location.getLatitude();
                        lon = location.getLongitude();

                }
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {}
                @Override
                public void onProviderEnabled(String provider) {}
                @Override
                public void onProviderDisabled(String provider) {}
            };
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);

在谷歌地图上显示:

Intent intent = null,chooser = null;
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:"+lat+","+lon));
chooser = Intent.createChooser(intent,"Launch Maps");
startActivity(chooser);



/* creating chooser because:
If this is not done, your app may crash when run on the emulator because the emulator may not have an activity installed which can handle the intent.*/

【讨论】:

  • 确保您在 Manifest 文件中拥有所需的权限。如果你觉得这很有用,请点击勾号并点赞 ;)
【解决方案2】:

试试这个

LocationManager lm = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Location net_loc = null;
lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

您可以从net_loc获取当前位置。

【讨论】:

  • 好的先生,让我试试这个,你知道我实际上是 android 的新手
  • 好的,先生,让我试试这个,实际上我是 android 新手,如果对此有任何疑问,请指导我。
  • 确定@Manoj 别担心。
  • 先生,我尝试使用您提供的上述代码,但它向我显示任何问题都没有问题可以给出一些解释代码继续进行。
  • 我应该为 net_location 设置什么
猜你喜欢
  • 2017-03-06
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多