1

i create one map in this i show one marker on current location its working fine when location turn on already but when Activity is on and i turn On location it does not show any marker i want to show marker when location is on any help?..

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
         getCurrentLocation();
       // latLngCurrentLocation is my current location which is update using getCurrentLocation() method.

    if(latLngCurrentLocation != null) {

    // add marker on current location
            markerCurrent = mMap.addMarker(new MarkerOptions()
                    .position(latLngCurrentLocation)
                    .draggable(true)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                    .title("Current Location"));

            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLngCurrentLocation));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(8));
            mMap.getUiSettings().setZoomControlsEnabled(true);
            mMap.setOnMarkerDragListener(this);


        }

    }

2 Answers2

2

You will get location in onLocationChanged(Location location) once your position is changed. You will get Latitude and Longitude in location object, so you can set your marker into onLocationChanged(Location location) method

@Override
public void onLocationChanged(Location location)
{
    mLastLocation = location;
    if (mCurrLocationMarker != null)
    {
        mCurrLocationMarker.remove();
    }

    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);

    //move map camera
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));


}

OR if you want to refresh your marker once GPS turns on then you can use BroadcastReceiver. BroadcastReciever triggers when GPS turns/off so you can check whether GPS is turned on or off, You can find example in following link, Broadcast receiver for GPS

Kuls
  • 2,047
  • 21
  • 39
  • thanks for your quick reply but i really want that without using OnLocation change method – nilesh prajapati Dec 05 '17 at 12:08
  • post your `getCurrentMethod()` code @nileshprajapati – Kuls Dec 05 '17 at 12:09
  • its method for get current Location there where i only used Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); – nilesh prajapati Dec 05 '17 at 12:11
  • You can use timer for that. You need to get function from particular time interval. @nileshprajapati – Kuls Dec 05 '17 at 12:12
  • using timer i can method more time its not good for app its consume more memory and battery also . – nilesh prajapati Dec 05 '17 at 12:15
  • is there i get any trigger when location turn on/off ? – nilesh prajapati Dec 05 '17 at 12:16
  • That's why Location API introduced. You shoud use` FusedLocationAPI`. @nileshprajapati – Kuls Dec 05 '17 at 12:16
  • its not issue to get current location issue is that after turn on location Refresh map – nilesh prajapati Dec 05 '17 at 12:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160511/discussion-between-kulsdroid-and-nilesh-prajapati). @nileshprajapati – Kuls Dec 05 '17 at 12:19
  • @nileshPrajapati onLocationChanged() is what you need as this way you can place a marker without any timer or handler as onLocationChanged will automatically be called when the location is updated which depends on Internet Speed as your 10 sec timer may work on fast internet connection but will not work on a slow connection which takes 1-2 minutes to load the location. So conclusively, onLocationChanged() will work exactly the way you want. – Lalit Fauzdar Dec 05 '17 at 12:33
  • OnLocationChange() not trigger quickly for that user need to change there location and i don't want that @LalitSinghFauzdar – nilesh prajapati Dec 05 '17 at 12:59
  • `onLocationChanged()` is called automatically after some specified delay (mostly 10 sec) after loading the initial location without even change in the physical location. Also, instead of using it, you only have the handler left for getting what you want. Just initialize a handler with 5 sec and re-call it unlit the location Latlng is not null. – Lalit Fauzdar Dec 05 '17 at 14:05
2

Through GpsTracker class or googleClient api you can get latitude and longtitude and then get it as gpstracker.getlatitude and getlongtitude and set this values to global variables and access it whereever you want.

Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
  • is there i get any trigger when location turn on/off ? – nilesh prajapati Dec 05 '17 at 12:16
  • Cant get your question. – Shivam Oberoi Dec 05 '17 at 12:17
  • if user turn On his location any default method or anything else which is trigger from that we can identify user turn on/off location – nilesh prajapati Dec 05 '17 at 12:20
  • you need to add a dialog that asks users to turn on gps everytime if gps if off.Please refer to the link [Link](https://stackoverflow.com/questions/4721449/how-can-i-enable-or-disable-the-gps-programmatically-on-android) – Shivam Oberoi Dec 05 '17 at 12:22
  • i all ready done this thing for tun on location but after turn on location i come back to map app there where no marker is added for that i need close app and restart it to see marker but i don"t want to do that @Shivam – nilesh prajapati Dec 05 '17 at 12:25