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?

4

1 回答 1

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.
于 2017-10-15T23:58:13.900 回答