简体   繁体   中英

How to get my phone's current location using Google Maps Api in Android?

I want to get a marker on my current location using Google map Api. I tested it on an Android phone and I am getting only a map. i don't get a marker on my current location. Even the toast what I have put in the code doesn't show. Can anyone help me with this. I am posting my code below:

public class MainActivity extends Activity implements LocationListener {
GoogleMap map;

@Override
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
}

 @Override
   public void onLocationChanged(Location location) {

 map.clear();

MarkerOptions mp = new MarkerOptions();

  mp.position(new LatLng(location.getLatitude(), location.getLongitude()));

  mp.title("my position");

   map.addMarker(mp);
   Toast.makeText(getApplicationContext(),
       location.getLatitude() +", "+location.getLongitude(),
       Toast.LENGTH_LONG).show();

  map.animateCamera(CameraUpdateFactory.newLatLngZoom(
   new LatLng(location.getLatitude(), location.getLongitude()), 16));

  }  }

Just remove map.clear(); from your code and add marker.remove();

This is working for me.. try this,

protected void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                //actionBar.getActionBar();



                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                System.out.println("current "+locationManager);

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 1, this);
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                addGoogleMap();
                addMarkers();

            }

            @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") 
            private void addGoogleMap() {
                if(googleMap == null){
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                googleMap.setOnMarkerClickListener(this);
                  googleMap.setOnMarkerDragListener(this);
                }

            }


            private void addMarkers() {

                if(googleMap != null)
                {
                lt = new LatLng(location.getLatitude(), location.getLongitude());


                marker = googleMap.addMarker(new MarkerOptions().position(lt)
                          .title("Current location ").snippet("Race Start: 9:00 AM CST")
                          .draggable(false));

                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                          lt, 20));
                }

            }

@Override
            public void onLocationChanged(Location location) {
            //txtLat = (TextView) findViewById(R.id.textview1);
            //txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
                System.out.println("loc changed");
                //googleMap.clear();
                marker.remove();
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                System.out.println("current "+locationManager);

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 1, this);
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                lt = new LatLng(location.getLatitude(), location.getLongitude());
                marker = googleMap.addMarker(new MarkerOptions().position(lt)
                          .title("Current location ").snippet("Race Start: 9:00 AM CST")
                          .draggable(false));
}

There is a in built method given by Google Map, if you want to get location using google map.

map.getLatitude();
map.getLongitude();

Try it, it will work.

您已经可以通过以下方式获取位置: location.getLatitude(), location.getLongitude()

打开您的移动GPS,当它停止闪烁时,表明它已找到位置,然后标记将显示。

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