2017-10-15 84 views
2

我將PlacePicker小部件添加到我的應用程序,現在我可以獲取選定的位置經度和緯度。 有了這個代碼: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請求,Google地圖,讓近的地方是一回事嗎?

但我在地圖上看到Picker用藍點找到我的設備,我想知道是否可以獲取我的設備的座標,或者我需要獲取設備座標的其他東西。

如果我需要別的東西來檢索設備位置,這是使用3G連接和GSP來做這件事的最佳方式?

無論如何,獲得千米距離和時間旅行的唯一方法是使用HTTP請求?

+2

如果位置已啓用,您可以用'onLationChanged'方法來獲得當前所在位置的經緯度,你也可以使用你的位置和所選地點的位置,通過使用方向HTTPS請求找到公里的距離和時間。獲得當前位置的最佳方式是FusedLocationProvider,它對3g和gps非常有用。 –

+0

啓用位置在哪裏? (抱歉,我是新的谷歌地圖API) 你可以發佈一些代碼? 好吧,所以獲得道路距離的唯一方法是使用HTTP請求和一些JSON解析器來獲取信息 – Dario

+1

我說過,根據您的帖子,您已經說過placepicker會發現我的設備有藍點。這意味着該位置已啓用。是的,距離和時間與解析通過https請求從谷歌地圖獲得的信息的json解析器一起工作。 –

回答

0

在Android應用中獲取位置的最佳方式是使用Google Play服務位置API,文檔編號爲https://developer.android.com/training/location/index.html

您可以通過一次調用來檢索設備的最後一個已知位置,以及添加一個監聽器以隨時間接收設備位置更新的方法。

就你而言,你可能想要檢索設備的最後一個已知位置。有關代碼示例的詳細分步文檔,請參閱https://developer.android.com/training/location/retrieve-current.html

  • 您需要按照「創建位置服務客戶端」部分所述創建融合位置提供程序客戶端對象。您將在onCreate()方法中添加該代碼。
  • 要真正獲取位置,請使用「獲取最近已知位置」部分中的示例代碼。顯示Toast消息後,您將此代碼放入onActivityResult()方法中。
+0

更改位置設置和獲取最後一個已知位置之間的區別是什麼? 我只是第二個? 請問你可以發佈一些代碼示例,因爲文檔比較差。 我需要我發佈的片段中的用戶位置。 – Dario

+0

我更新了我的答案,以提供有關在哪裏可以找到示例代碼以及如何將其集成到您的應用中的更多詳細信息。 –

+0

非常感謝! – Dario

相關問題