简体   繁体   中英

Get user current location and determine it in Google maps

i wan to ask you: i am using google Maps in my application and when i open the activity that has the map i want the user current location to be determine, i am using this code to retrieve the current user location

         public void locat ()
        {
       //Use the LocationManager class to obtain GPS locations 

        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        LocationListener mlocListener = new MyLocationListener();


        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);


        }


        // Class My Location Listener 

        public class MyLocationListener implements LocationListener

        {

        @Override

        public void onLocationChanged(Location loc)

        {

        loc.getLatitude();

        loc.getLongitude();

        String Text = "My current location is: " + "Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();


        Toast.makeText( getApplicationContext(),

        Text,

        Toast.LENGTH_SHORT).show();

        }


        @Override

        public void onProviderDisabled(String provider)

        {

        Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();

        }


        @Override

        public void onProviderEnabled(String provider)

        {

        Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();

        }


        @Override

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

        {


        }

        }// End of Class MyLocationListener */

but it is just give me the latitude and longitude and that is not what i want! i want to determine the user current location in google maps!how can i do it ? do you need my code to google MAps? Please please help me! THANK YOU

If I understand your question correctly you want to get the users current position and show it on a map. You have the latitude and longitude values, all you need to do is construct a GeoPoint and animate to that position on the map. You can also add a custom overlay to the map if desired. Something like the following should help.

double lat = location.getLatitude();
double longi = location.getLongitude();
long time = location.getTime();
GeoPoint currentGeo = new GeoPoint(toMicroDegrees(lat), toMicroDegrees(longi));
MapController controller = map.getController();
controller.animateTo(currentGeo);

You can open google maps with a geo uri like so:

    String url = "geo:48.200927,16.369548,192";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

Edit: It appears that you want reverse geocoding. Here is an example:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);

Here is the documentation for the android Geocoder class - http://developer.android.com/reference/android/location/Geocoder.html

You are never actually passing the information to a map in order to display the location.

What you need to do is get the MapView from the layout you displaying.

Then get the controller of the map view and set the users location as the centre/animate to their location.

Something similar to this:

mapView1 = (MapView) this.findViewById(R.id.map);
MapController mapControl = mapView1.getController();
GeoPoint geo = new GeoPoint((int)(lat)*1e6), (int(long*1e6));
mapControl.animateTo(geo);

This link should help http://mobiforge.com/developing/story/using-google-maps-android

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