PlacePicker获取设备位置和选定的距离和时间道路的位置

时间:2017-10-15 17:40:16

标签: android google-maps gmsplacepicker

我将PlacePicker小部件添加到我的应用程序中,现在我可以获得选定的位置纬度和经度。 使用此代码:

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();
            }
        }
    }

}

如果我很清楚使用HTTP请求谷歌地图到达附近是一回事吗?

但是我在地图上看到Picker找到了我的设备有一个蓝点,我想知道是否有可能得到我的设备的坐标或我需要别的东西来获取设备坐标。

如果我需要其他东西来检索设备位置,这是通过使用3G连接和GSP实现这一目标的最佳方式吗?

无论如何,要获得Km距离和时间旅行,唯一的方法是使用HTTP请求吗?

1 个答案:

答案 0 :(得分:0)

在Android应用中获取位置的最佳方式是使用https://developer.android.com/training/location/index.html中记录的Google Play服务位置API。

您可以进行一次调用以检索设备的最后已知位置,以及添加侦听器以随时间接收设备位置更新的方法。

在您的情况下,您可能只想检索设备的最后已知位置。有关代码示例的详细分步文档可在https://developer.android.com/training/location/retrieve-current.html获得。

  • 您需要创建一个Fused Location Provider Client对象,如“创建位置服务客户端”部分所述。您将在onCreate()方法中添加该代码。
  • 要实际获取该位置,请使用“获取最后一个已知位置”部分中的示例代码。在显示Toast消息后,您将把此代码放在onActivityResult()方法中。