如何使用谷歌地图在Android中的当前位置?

时间:2014-05-27 10:06:21

标签: android google-maps geolocation

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

如果有人对此有任何疑问,请帮助我。

2 个答案:

答案 0 :(得分:2)

您可以根据纬度和经度获取用户的当前位置,只需制作隐式意图即可使用这些值启动地图。

获取当前位置:

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.*/

答案 1 :(得分:0)

试试这个

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

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