简体   繁体   中英

How to current location in Android using google Maps?

I'm trying to get the user's Location by showing that he is in which area with Particular location in google maps with the help of Android API while surfing in internet I came to know that getLocationManger() is used to view the Location but it shows only by Latitude,Longitude.

If someone have any idea about this please help me friends.

You can get the user's current location in terms of latitude and longitude and simply make an Implicit Intent to launch map with these values.

To get current location:

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);

To show on google map:

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

Try this one

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

you can get the current location from net_loc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM