0
有時getFromlocationName()方法返回null,有時它會給出值,但繪製的路徑不正確。這可能是什麼原因?好心解決問題,謝謝。谷歌地圖顯示錯誤和不完整的方向
下面是代碼
if((place1 != null) || (!place1.equals(" "))&& ((place2!=null)||(!place2.equals(" "))))
{
Geocoder geocoder = new Geocoder(MapsActivity.this);
try {
Boolean check=geocoder.isPresent();
Log.d("check:"," "+check);
addressList1 = geocoder.getFromLocationName(place1, 1);
addressList2 = geocoder.getFromLocationName(place2, 1);
} catch (IOException e) {
e.printStackTrace();
}
Log.d("ADDRESS","address obj:"+addressList1+","+addressList2);
Address address1 = addressList1.get(0);
Address address2 = addressList2.get(0);
origin = new LatLng(address1.getLatitude(),address1.getLongitude());
destination = new LatLng(address1.getLatitude(),address2.getLongitude());
Log.d("LATLANG","LatLang:"+origin+","+destination);
build_retrofit_and_get_response(mode);
}
}
public void build_retrofit_and_get_response(String type) {
Toast.makeText(this,"retrofit method called"+origin+destination,Toast.LENGTH_LONG).show();
String url = "https://maps.googleapis.com/maps/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitMaps service = retrofit.create(RetrofitMaps.class);
Call<Example> call = service.getDistanceDuration("metric", origin.latitude + "," + origin.longitude,destination.latitude + "," + destination.longitude, type);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Response<Example> response, Retrofit retrofit) {
try {
//Remove previous line from map
if (line != null) {
line.remove();
}
// This loop will go through all the results and add marker on each location.
for (int i = 0; i < response.body().getRoutes().size(); i++) {
String distance = response.body().getRoutes().get(i).getLegs().get(i).getDistance().getText();
String time = response.body().getRoutes().get(i).getLegs().get(i).getDuration().getText();
ShowDistanceDuration.setText("Distance:" + distance + ", Duration:" + time);
String encodedString = response.body().getRoutes().get(0).getOverviewPolyline().getPoints();
List<LatLng> list = decodePoly(encodedString);
line = mMap.addPolyline(new PolylineOptions()
.addAll(list)
.width(10)
.color(Color.RED)
.geodesic(true)
);
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat/1E5)),
(((double) lng/1E5)));
poly.add(p);
}
return poly;
}
看看http://stackoverflow.com/questions/39910838/map-v2-drawed-polylines-are-not-exactly-on-the-road/39911889#39911889 – antonio