0

I add the PlacePicker widget to my application and now i'm able to get selected location latitude and longitude. With this code:

public class TravelFragment extends Fragment {

    int PLACE_PICKER_REQUEST = 1;
    TextView lat;
    TextView lng;

    public TravelFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final FragmentActivity activity = getActivity();

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_travel, container, false);

        lat = (TextView) view.findViewById(R.id.latitudeValue);
        lng = (TextView) view.findViewById(R.id.longitudeValue);

        Button findLocation = (Button) view.findViewById(R.id.buttonSearchLocation);

        findLocation.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
                try {
                    startActivityForResult(builder.build(activity), PLACE_PICKER_REQUEST);
                } catch (GooglePlayServicesRepairableException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }
            }
        });


        return view;
    }

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {

                double arrivalLat;
                double arrivalLng;
                String stringLat;
                String stringLng;


                Place place = PlacePicker.getPlace(data, getActivity());
                arrivalLat = place.getLatLng().latitude;
                arrivalLng = place.getLatLng().longitude;
                stringLat = Double.toString(arrivalLat);
                stringLng = Double.toString(arrivalLng);

                lat.setText(stringLat);
                lng.setText(stringLng);

                String toastMsg = String.format("Place: %s",  place.getLatLng());

               // place.getLatLng();

                Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show();
            }
        }
    }

}

And if i well understand this is the same thing to use HTTP request to google maps for get near place?

But i see in the map that the Picker find my device with a blue dot, i would like to know if it possible to get the coordinates of my device or i need something else for getting device coordinates.

If i need something else for retrieve device position which is the best way for doing that by using 3G connection and GSP?

Anyway for getting Km distance and time travel the only way is to use the HTTP request?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dario
  • 732
  • 7
  • 30
  • 2
    If Location is enabled, you can use `onLationChanged` method to get LatLng of your current location and you can also use your location and selected place's location to find distance and time in Kms by using https request for direction. And the best way to get current location is FusedLocationProvider and it will work great with 3g and gps. – Lalit Fauzdar Oct 15 '17 at 17:48
  • Location enabled where? (sorry but i'm new with Google maps API) Can you post some code? Ok so the only way to get road distance is with HTTP request and some JSON parser for get information – Dario Oct 15 '17 at 18:04
  • 1
    I said that on the basis of your post as you've said placepicker finds my device with a blue dot. This means that location is enabled. And yes, distance and time works with a json parser which parse the information it gets from google maps through https request. – Lalit Fauzdar Oct 16 '17 at 02:50

1 Answers1

0

The best way to get location in an Android app is to use the Google Play Services location API, documented at https://developer.android.com/training/location/index.html.

There is a single call you can make to retrieve the device's last known location and a way to add a listener to receive updates on the device's position over time.

In your case, you probably want to just retrieve the device's last known location. Detailed step-by-step documentation with code samples is available at https://developer.android.com/training/location/retrieve-current.html.

  • You will need to create a Fused Location Provider Client object as described in the "Create Location Services Client" section. You will add that code in your onCreate() method.
  • To actually get the location, use the sample code in the "Get the Last Known Location" section. You will put this code in your onActivityResult() method, after you show the Toast message.
Scott Kronheim
  • 762
  • 6
  • 7
  • Which are the difference between Change Location Settings and Getting the Last Known Location? I just the the second one? And can you post some code example because the documentation is poor of example, please. I need the user location in the fragment that i post. – Dario Oct 16 '17 at 09:36
  • I updated my answer to supply more detail on where to find sample code and how to integrate it into your app. – Scott Kronheim Oct 16 '17 at 11:49