how to get current location in google map android

skyshine

Actually my problem is I am not getting current location latitude and longitude I tried so many ways.I know that this question already asked in SO I tried that answers also still I didn't get answer.Please help me Code:

    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
    googleMap.setMyLocationEnabled(true);
    Location myLocation = googleMap.getMyLocation();  //Nullpointer exception.........
    LatLng myLatLng = new LatLng(myLocation.getLatitude(),
            myLocation.getLongitude());

    CameraPosition myPosition = new CameraPosition.Builder()
            .target(myLatLng).zoom(17).bearing(90).tilt(30).build();
    googleMap.animateCamera(
        CameraUpdateFactory.newCameraPosition(myPosition));
Shailendra Madda

Please check the sample code for the Google Maps Android API v2. Using this will solve your problem.

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.setMyLocationEnabled(true);
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
                @Override
                public void onMyLocationChange(Location arg0) {
                    mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
                }
            });
        }
    }
}

Call this function in onCreate function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get Marker Location From Current Location Android Google Map

From Dev

Android: Not able to get current location in Google Map fragment

From Dev

How can I show current location on a Google Map on Android Marshmallow?

From Dev

Not able to get current location in Google Map fragment

From Dev

Unable to get current location with google map

From Java

How to get current location in Android

From Dev

How to show my current location in Google Map Android using google API client

From Dev

How to get Current location through google API?

From Dev

Android Google Map get correct location not work

From Dev

How to disable google map current location tracking after first loading android

From Dev

Google Map get current location that can be changed by dragging the pin

From Dev

google map ios sdk: could not get current user location?

From Dev

Google Map get current location that can be changed by dragging the pin

From Java

How to get the current location latitude and longitude in android

From Dev

How to get user's current location in Android

From Dev

How to get Accurate Current location in android?

From Dev

How to get the current location temperature in android

From Dev

Android Location - how to get current and avoiding nullpointerexception

From Dev

How to get Accurate Current location in android?

From Dev

How to get user's current location in Android

From Dev

How to get my current location in android maps?

From Dev

Set google map to current location

From Dev

How to get screen center location of google map?

From Dev

How to get screen center location of google map?

From Dev

get current map location when open android searchView widget

From Dev

How to auto update current location on google map in ionic 2 application

From Dev

How remove default google map current location icon?

From Dev

How to make circle on a current location in google map instead of marker

From Dev

How to get Current GPS location(latitude-longitude) using Google API in Android App?

Related Related

  1. 1

    Get Marker Location From Current Location Android Google Map

  2. 2

    Android: Not able to get current location in Google Map fragment

  3. 3

    How can I show current location on a Google Map on Android Marshmallow?

  4. 4

    Not able to get current location in Google Map fragment

  5. 5

    Unable to get current location with google map

  6. 6

    How to get current location in Android

  7. 7

    How to show my current location in Google Map Android using google API client

  8. 8

    How to get Current location through google API?

  9. 9

    Android Google Map get correct location not work

  10. 10

    How to disable google map current location tracking after first loading android

  11. 11

    Google Map get current location that can be changed by dragging the pin

  12. 12

    google map ios sdk: could not get current user location?

  13. 13

    Google Map get current location that can be changed by dragging the pin

  14. 14

    How to get the current location latitude and longitude in android

  15. 15

    How to get user's current location in Android

  16. 16

    How to get Accurate Current location in android?

  17. 17

    How to get the current location temperature in android

  18. 18

    Android Location - how to get current and avoiding nullpointerexception

  19. 19

    How to get Accurate Current location in android?

  20. 20

    How to get user's current location in Android

  21. 21

    How to get my current location in android maps?

  22. 22

    Set google map to current location

  23. 23

    How to get screen center location of google map?

  24. 24

    How to get screen center location of google map?

  25. 25

    get current map location when open android searchView widget

  26. 26

    How to auto update current location on google map in ionic 2 application

  27. 27

    How remove default google map current location icon?

  28. 28

    How to make circle on a current location in google map instead of marker

  29. 29

    How to get Current GPS location(latitude-longitude) using Google API in Android App?

HotTag

Archive