user3066085
user3066085

Reputation: 128

google map v2 in android how to get current position automaticaley and main countryes notification

google map v2 in android how to get current position and zoom in automatically and main countries selection point notification

setContentView(R.layout.activity_main);

     try {

            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

}
     @SuppressLint("NewApi")
    private void initilizeMap() {
            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(

                        R.id.map)).getMap();

googleMap.animateCamera(CameraUpdateFactory.zoomTo(5.0f));

                LocationManager mlocManager = 

(LocationManager)getSystemService(Context.LOCATION_SERVICE);

                googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(0, 0))
                .title("Your Location is"));



                        if (googleMap == null) {
                    Toast.makeText(getApplicationContext(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        } 
        protected void onResume() {
            super.onResume();
            initilizeMap();
        } 

Upvotes: 0

Views: 488

Answers (1)

owe
owe

Reputation: 4930

After analysing your question you could try this:

private LatLng getCurrentPosition(){
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();             // set criteria for location provider:
    criteria.setAccuracy(Criteria.ACCURACY_FINE);   // fine accuracy
    criteria.setCostAllowed(false);                 // no monetary cost

    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    LatLng myPosition= new LatLng(location.getLatitude(), location.getLongitude());
    Toast.makeText(this, "current position: "+ location.getLatitude() + "," + location.getLongitude(), Toast.LENGTH_SHORT).show();
    return myPosition; 
}

Upvotes: 2

Related Questions