简体   繁体   中英

How to get current location on google maps using map view

I want to know that how I can get my current position on google maps using map view. That is when I walk and move from one location to other so my location on maps will also be changed. It will be achieved by GPS or by Network Provider but want to get my appropriate current location.

you must implement Location Listener to get the latitude and longitude data,using those values set the position on google map and use a marker to indicate that position

public class MyLocationListener implements LocationListener {
protected LocationManager lm;
public String message="Hello";
Location location;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; 
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000000; 
Context mContext;
public MyLocationListener(Context mContext) {
    // TODO Auto-generated constructor stub
    this.mContext = mContext;
    String context=Context.LOCATION_SERVICE;
    lm = (LocationManager)mContext.getSystemService(context);
    lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,this);
}


  public void showCurrentLocation() 
  {

        location=lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
       if (location != null) {
             message = String.format(
                    "Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );

           t1.setText(message);

      }


    }   

  public void onLocationChanged(Location location) 
  {
    /*  String message = String.format(
              "New Location \n Longitude: %1$s \n Latitude: %2$s",
              location.getLongitude(), location.getLatitude()
      );
      t1.setText(message);
    Toast.makeText(WinUirep.this,
              "Location Changed !",
              Toast.LENGTH_LONG).show();*/

  }

  public void onStatusChanged(String s, int i, Bundle b) 
  {

  }

  public void onProviderDisabled(String s) 
  {

  }

  public void onProviderEnabled(String s) 
  {

  }}

location.getlatitude() gets the latitude from the.network provider, sames goes for longitude

use LocationListener to get the currentLocation latitude longitude

public class MyLocationListener implements LocationListener{
    private Context context;

    public MyLocationListener (Context context){

            this.context = context;
            this.frequency = frequency;

            locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

            criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            criteria.setAltitudeRequired(false);
            criteria.setCostAllowed(true);
            criteria.setSpeedRequired(false);
            criteria.setBearingRequired(false);
            criteria.setPowerRequirement(Criteria.POWER_HIGH);

            provider = locationManager.getBestProvider(criteria, true);

            location = locationManager.getLastKnownLocation(provider);

    }
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        // here you can get latitute and longitude and send to mapview to show the current location on map
            double latitute = location.getLatitude();
            double longitude = location.getLongitude();
        Toast.makeText(context, "lat,long:"+latitute+","+longitude, Toast.LENGTH_LONG);

    }

}

see the tutorial at click here to read the tutorial to get the current latitude and longitude

click here to read how to put icon on map on particular latitude and longitude

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