简体   繁体   中英

Adding a My Location Button in google maps on my android app

I'm trying to add the my location button in my android application. This is the code I currently have:

public class MapActivity extends FragmentActivity {


    private GoogleMap googleMap;
    int position;
    String alllatitude,alllongitude,alldirname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_activity);

        Intent i=getIntent();

        position=i.getIntExtra("POSITION", 0);

        alllatitude=i.getStringExtra("LATITUDE");
        alllongitude=i.getStringExtra("LONGITUDE");
        alldirname=i.getStringExtra("DIRNAME");
        LatLng TutorialsPoint = new LatLng(Double.parseDouble(alllatitude), Double.parseDouble(alllongitude));
        try {
            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager()
                        .findFragmentById(R.id.map)).getMap();
            }
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(TutorialsPoint, 15.0f));
            googleMap.addMarker(new MarkerOptions().position(TutorialsPoint)
                    .title(alldirname));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Any help would be much appreciated.

Use this to display the default location button on the Google Maps V2 API for android.

googleMap.setMyLocationEnabled(true); 

googleMap.getUiSettings().setMyLocationButtonEnabled(true);

If you just want the basic location button, such as the one on the Google Maps app, do the following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setUpMapIfNeeded();

    // Used for finding current location with button
    map.setMyLocationEnabled(true);
    map.getUiSettings().setMyLocationButtonEnabled(true);
}

Let me know if this helps.

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