-1

I am following these awesome instructions on how to draw directions from origin to destination it almost works as I want. I have a strange problem.

When I want to set my longitude and latitude like follows I get a crash:

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
LatLng origin = new LatLng(location.getLatitude(),location.getLongitude());

But when I write numbers like (60.1,14.2) where I have the long and lat which is just a random location it works.

Why is this and how can it be fixed?

LogaCat

04-30 15:43:05.950 818-818/mawemo.stadsguiden E/AndroidRuntime: FATAL EXCEPTION: main Process: mawemo.stadsguiden, PID: 818 java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference at mawemo.stadsguiden.MainActivity.addMarkersRoute(MainActivity.java:324) at mawemo.stadsguiden.MainActivity.onMapReady(MainActivity.java:420) at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source) at android.os.Binder.transact(Binder.java:395) at com.google.android.gms.maps.internal.v$a$a.a(:com.google.android.gms.alldynamite:82) at maps.ei.bu$6.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6117) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

UPDATE

Why the minus? You just can't hand out minus without a reason

UPDATE 2

I have tested to declare two global variables like this

Double lat = 0.0;
Double lon = 0.0;

And then try to pass the values in with Location as follows:

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

lat = location.getLatitude();
lon = location.getLongitude();

So please dear Stack Overflow tell me what I am missing.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
codemoonger
  • 613
  • 3
  • 11
  • 22
  • As the crash log said, getLastLocation() method returns null. It means, fused provider has no previously received location. The solution, request for location updates. You can check this link https://guides.codepath.com/android/Retrieving-Location-with-LocationServices-API – blackkara Apr 30 '16 at 16:26
  • @Blackkara can you post comment as answer? – codemoonger Apr 30 '16 at 17:50

1 Answers1

2

getLastLocation() method retuns null. Because fused provider has no previously received location

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

So, then when you attempt to use like below, throwing exception.

LatLng origin = new LatLng(location.getLatitude(),location.getLongitude());

You need to make location request, Check this link

blackkara
  • 4,900
  • 4
  • 28
  • 58