Android 导航意图 - 我怎样才能确保它将我导航到正确的地方?
Android Intent for Navigation - how can I ensure it will navigate me to the right place?
我正在开发一个应用程序,它可以让用户保存目的地,然后单击导航到该目的地。在很多情况下,输入的位置(由 Google Place API 自动完成提供)然后点击导航,将我带到错误的地方。
一个例子是寻找乔治·布什洲际机场。当我开始打字时,自动完成建议是:
乔治·布什洲际机场,北航站楼路,休斯敦德克萨斯州,美国
但是,当我启动意图导航到此位置时:
new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + mapData.getPlace());
其中 mapData.getPlace() 是一个等于上述地址的字符串,导航启动并将我引导至 William P. Hobby 机场。
这是另一个例子。当我搜索 Hobby Airport 时,Place 自动完成提示:
美国德克萨斯州休斯顿机场大道霍比机场
但是,当我启动一个导航意图时(如上所述),它会将我导航到
休斯顿霍比机场希尔顿逸林酒店
自动完成提供的地址似乎不是可导航的地址,导航意图选择最接近的建议。
我尝试过的一种方法是过滤自动完成结果,例如 AutoCompleteFilter(如此处 https://developers.google.com/places/android/autocomplete 所示)。我的代码如下:
List<Integer> filterTypes = new ArrayList<Integer>();
filterTypes.add(Place.TYPE_STREET_ADDRESS);
AutocompleteFilter filter = AutocompleteFilter.create(filterTypes);
// Create and attach adapter
mAdapter = new PlaceAutocompleteAdapter(context, android.R.layout.simple_list_item_1,
mGoogleApiClient,MainActivity.CURRENT_BOUNDS , filter);
但这经常会导致 "Error getting autocomplete prediction API call: Status{statusCode=NETWORK_ERROR, resolution=null}"
我通过对自动完成的结果进行地理编码解决了这个问题。我使用 Geocoder 对象并采纳它提供的第一个建议。
Geocoder geocoder = new Geocoder(context, Locale.US);
try {
// Take first suggestion from geocoder object
List<Address> coordinates = geocoder.getFromLocationName(mapData.getPlace(),1);
//If a geocode result is supplied, use it. Otherwise, use the string from mapData
if (coordinates.size() > 0){
double latitude = coordinates.get(coordinates.size() - 1).getLatitude();
double longitude = coordinates.get(coordinates.size() - 1).getLongitude();
coords = (latitude + "," + longitude);}
else{
coords = mapData.getPlace();}
然后我将坐标传递到导航意图中。
我正在开发一个应用程序,它可以让用户保存目的地,然后单击导航到该目的地。在很多情况下,输入的位置(由 Google Place API 自动完成提供)然后点击导航,将我带到错误的地方。
一个例子是寻找乔治·布什洲际机场。当我开始打字时,自动完成建议是:
乔治·布什洲际机场,北航站楼路,休斯敦德克萨斯州,美国
但是,当我启动意图导航到此位置时:
new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + mapData.getPlace());
其中 mapData.getPlace() 是一个等于上述地址的字符串,导航启动并将我引导至 William P. Hobby 机场。
这是另一个例子。当我搜索 Hobby Airport 时,Place 自动完成提示:
美国德克萨斯州休斯顿机场大道霍比机场
但是,当我启动一个导航意图时(如上所述),它会将我导航到
休斯顿霍比机场希尔顿逸林酒店
自动完成提供的地址似乎不是可导航的地址,导航意图选择最接近的建议。
我尝试过的一种方法是过滤自动完成结果,例如 AutoCompleteFilter(如此处 https://developers.google.com/places/android/autocomplete 所示)。我的代码如下:
List<Integer> filterTypes = new ArrayList<Integer>();
filterTypes.add(Place.TYPE_STREET_ADDRESS);
AutocompleteFilter filter = AutocompleteFilter.create(filterTypes);
// Create and attach adapter
mAdapter = new PlaceAutocompleteAdapter(context, android.R.layout.simple_list_item_1,
mGoogleApiClient,MainActivity.CURRENT_BOUNDS , filter);
但这经常会导致 "Error getting autocomplete prediction API call: Status{statusCode=NETWORK_ERROR, resolution=null}"
我通过对自动完成的结果进行地理编码解决了这个问题。我使用 Geocoder 对象并采纳它提供的第一个建议。
Geocoder geocoder = new Geocoder(context, Locale.US);
try {
// Take first suggestion from geocoder object
List<Address> coordinates = geocoder.getFromLocationName(mapData.getPlace(),1);
//If a geocode result is supplied, use it. Otherwise, use the string from mapData
if (coordinates.size() > 0){
double latitude = coordinates.get(coordinates.size() - 1).getLatitude();
double longitude = coordinates.get(coordinates.size() - 1).getLongitude();
coords = (latitude + "," + longitude);}
else{
coords = mapData.getPlace();}
然后我将坐标传递到导航意图中。