Faisal
Faisal

Reputation: 604

Get LatLong from HashMap in Java

static final HashMap<String, LatLng> BAY_AREA_LANDMARKS = new HashMap<String, LatLng>();

static {
    // Office Location.
    BAY_AREA_LANDMARKS.put("Office", new LatLng(  31.112635, 73.114867));
    // House Location.
    BAY_AREA_LANDMARKS.put("Home", new LatLng(  31.138056, 73.108787));
    //College Location.
    BAY_AREA_LANDMARKS.put("College", new LatLng( 31.104999,73.114727));
    // Uncle's House Location.
    BAY_AREA_LANDMARKS.put("Uncle", new LatLng( 31.137073,73.105472));
    // Code Debuggers Location.
    BAY_AREA_LANDMARKS.put("Code Debuggers", new LatLng( 31.117376,73.117588));
}

There are my Hash Locations I want LatLong Value by string("College","Home".....)

Here is code which I am trying

public void addGeofencesButtonHandler(View view) {
    if (checkPermissions()) {
        Map<String , LatLng> Loc= new HashMap<String, LatLng>();
        LatLng ff =  Loc.get("College"); // return ff = null
        Toast.makeText(MainActivity.this, ff.toString(),Toast.LENGTH_SHORT).show();
    }
}

How can I get Lat and Long value and show them?

Upvotes: 1

Views: 150

Answers (1)

Michael
Michael

Reputation: 44250

You're declaring a new map then attempting to get a value from it. You've just created it so it's empty.

Map<String , LatLng> Loc= new HashMap<String, LatLng>();
LatLng ff =  Loc.get("College"); // return ff = null

You need to get the value from the map declared in your first snippet. Replace the two lines above with:

LatLng ff = BAY_AREA_LANDMARKS.get("College");

Upvotes: 1

Related Questions