Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

googleMap.getMyLocation(); cannot get current location

i want to make a program to calculate the distance between some places to my current location, but my googleMap.getMyLocation(); doesnt work properly.

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);   
googleMap.getUiSettings().setCompassEnabled(false);
mylocation = googleMap.getMyLocation();
direction = new GMapV2Direction();

LatLng from, to;
from = new LatLng(-7.26071409, 112.80674726);
for(int i=0; i<lat.length; i++){    
    to = new LatLng(lat[i], lon[i]);
    doc = direksi.getDocument(from, to, GMapV2Direction.MODE_DRIVING);
    distance[i] = (double)direction.getDistanceValue(doc) / 1000;
}

i saved the latitude and longitude of some places in lat[] and lon[]. LatLng 'from' is mylocation and 'to' is my destination places. the problem appear when i change

from = new LatLng(-7.26071409, 112.80674726);
to
from = new LatLng(mylocation.getLatitude(), mylocation.getLongitude());

i want to do the calculation without opening the googlemaps. the google maps will appear when i touch the map button as pop up window. so the calculation will happen without opening googlemap

please help me

like image 802
user3506223 Avatar asked Apr 16 '14 08:04

user3506223


People also ask

How do I enable Google Maps to use my location?

Check "Enable Location Services." On your computer, open Chrome and go to Google Maps. In the bottom right, click My location . If you see a message that says "Google Maps does not have permission to use your location," follow the steps below. In the top right, click More .

Why does Google Maps say it doesn't have permission to use?

If you see a message that says "Google Maps does not have permission to use your location," follow the steps below. In the top right, click More . Click Settings At the bottom, click Advanced. In the "Privacy and security" section, click Content settings.

Is getmylocation() method deprecated?

getMyLocation (), this method is deprecated. use LocationClient instead. are you using google map v2? @Alex i dont know how to use LocationClient. can you elaborate?

How to enable Google Maps on safari?

On your computer, open Safari and go to Google Maps. In the bottom right, click My location . When asked to share your location, choose Allow. You might be asked to change your location settings on your computer before you can turn them on for Safari. To do this: Open System Preferences Security & Privacy Preferences Privacy Location Services.


2 Answers

Try this...I hope this will be help to you

LocationManager locationManager = (LocationManager)
        getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager
        .getBestProvider(criteria, false));
double latitude = location.getLatitude();
double longitude = location.getLongitude();
like image 197
Android_coder Avatar answered Sep 19 '22 13:09

Android_coder


This is my code for you, I hope this help you... edit it if you think, it isn't with correct sintaxis...

This work for android 4.x and 5.x and 6.x, I not test this in android 7

//I use this like global var to edit or get informacion about google map
GoogleMap gMap;

@Override
public void onMapReady(GoogleMap googleMap) {
    gMap = googleMap;

if (ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(Activity_Map.this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                    }else{
                        if(!gMap.isMyLocationEnabled())
                            gMap.setMyLocationEnabled(true);

                        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                        Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if (myLocation == null) {
                            Criteria criteria = new Criteria();
                            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
                            String provider = lm.getBestProvider(criteria, true);
                            myLocation = lm.getLastKnownLocation(provider);
                        }

                        if(myLocation!=null){
                            LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
                            gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14), 1500, null);
                        }
                    }

}
like image 44
DarckBlezzer Avatar answered Sep 20 '22 13:09

DarckBlezzer