-2

I am trying to find out the latitude and longitude of other/desired locations. Please keep in mind that i am not asking about to get the latitude and longitude of current location.

Also i need to find out nearby places of other/desired locations?

Thanks in advance

1 Answers1

0

Just go to developers console and enable the places-api refer this link for enabling api or this link and do the following code

activity_main.xml

<fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

MainActivity.java

String location=null;
   PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);



        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.i(TAG, "Place===: " + place.getName());
                location=place.getName().toString();
 if(location!=null || !location.equals("")) {
                    Geocoder geocoder = new Geocoder(getApplicationContext());

                    try {
                        addresses = geocoder.getFromLocationName(location, 1);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Address address = addresses.get(0);

                    String place_name = addresses.get(0).getAddressLine(0);
                    String latitude=address.getLatitude();
                    String longitude= address.getLongitude());


                }

            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.i(TAG, "An error occurred===: " + status);
            }
        });

In manifest.xml add the following lines

 <uses-permission android:name="android.permission.INTERNET" />

and under application tag add

 <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

try this code.

Bhagyashri
  • 182
  • 1
  • 15