java - Google maps mark (current location) crashes the application, how to fix it? -


i trying add current location mark android google map application in android studio.

my code:

private void setupmap() { location location = null; latlng loc = new latlng(location.getlatitude(), location.getlongitude()); marker marker; marker = mmap.addmarker(new markeroptions().position(new latlng((location.getlatitude()), location.getlongitude())).title("marker"));} 

it causes application crash. underneath there mistakes appeared while running application:

http://postimg.org/image/qrxdo9dl5/full/

(i used postimage upload larger size screen-shot)


before modified code, had working code set position 0,0:

private void setupmap() { mmap.addmarker(new markeroptions().position(new latlng(0, 0)).title("marker")); 

then tried change location '0,0' read current latitude , longitude , show on google map. changed code based on post: how current location in google maps android api v2?

i java beginner , appreciated. thank help.

you getting nullpointerexception because location object set null. in linked answer location object comes location callback.

public void onmylocationchange(location location) // location object here 

you can location map reference so

location location = mmap.getmylocation(); 

however method deprecated , using location api encouraged. location api can follow guide:

https://developer.android.com/training/location/retrieve-current.html

you build googleapiclient, connect it, , use location.

location location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -