Google Directions APIを使用してマップビューにルートを表示しようとしていますが、JSON応答からデータを取得するのに問題があります。「レベル」と「ポイント」の文字列は取得できますが、マップ上のポイントにデコードする方法がわかりません。
どんな助けでも大歓迎です。
Google Directions APIを使用してマップビューにルートを表示しようとしていますが、JSON応答からデータを取得するのに問題があります。「レベル」と「ポイント」の文字列は取得できますが、マップ上のポイントにデコードする方法がわかりません。
どんな助けでも大歓迎です。
私はあなたのためにそれらをデコードできるクラスを持っています、以下のクラスを追加してから次のようにあなたのコードを呼び出してください:
int[] decodedZoomLevels = PolylineDecoder.decodeZoomLevels(levels);
GeoPoint[] gPts = PolylineDecoder.decodePoints(points, decodedZoomLevels.length);
ここでpoints
、levels
はJSON応答から抽出したデータです。次に、ジオポイントの配列を調べて、それらの間に線を引き、方向を表示します。
お役に立てれば!ケニー
編集:グーグルルートAPIはJSON応答の一部としてズームレベル文字列を返さないようですが、心配する必要はありませんが、これを使用していたのはポイント数をチェックすることだけだったので、これらを簡単に入れることができます次のようなリスト:
public static List <GeoPoint> decodePoints(String encoded_points){
int index = 0;
int lat = 0;
int lng = 0;
List <GeoPoint> out = new ArrayList<GeoPoint>();
try {
int shift;
int result;
while (index < encoded_points.length()) {
shift = 0;
result = 0;
while (true) {
int b = encoded_points.charAt(index++) - '?';
result |= ((b & 31) << shift);
shift += 5;
if (b < 32)
break;
}
lat += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
shift = 0;
result = 0;
while (true) {
int b = encoded_points.charAt(index++) - '?';
result |= ((b & 31) << shift);
shift += 5;
if (b < 32)
break;
}
lng += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
/* Add the new Lat/Lng to the Array. */
out.add(new GeoPoint((lat*10),(lng*10)));
}
return out;
}catch(Exception e) {
e.printStackTrace();
}
return out;
}
編集:古いコード
public class PolylineDecoder {
/**
* Transform a encoded PolyLine to a Array of GeoPoints.
* Java implementation of the original Google JS code.
* @see Original encoding part: <a href="http://code.google.com/apis/maps/documentation/polylinealgorithm.html">http://code.google.com/apis/maps/documentation/polylinealgorithm.html</a>
* @return Array of all GeoPoints decoded from the PolyLine-String.
* @param encoded_points String containing the encoded PolyLine.
* @param countExpected Number of points that are encoded in the PolyLine. Easiest way is to use the length of the ZoomLevels-String.
* @throws DecodingException
*/
public static GeoPoint[] decodePoints(String encoded_points, int countExpected){
int index = 0;
int lat = 0;
int lng = 0;
int cnt = 0;
GeoPoint[] out = new GeoPoint[countExpected];
try {
int shift;
int result;
while (index < encoded_points.length()) {
shift = 0;
result = 0;
while (true) {
int b = encoded_points.charAt(index++) - '?';
result |= ((b & 31) << shift);
shift += 5;
if (b < 32)
break;
}
lat += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
shift = 0;
result = 0;
while (true) {
int b = encoded_points.charAt(index++) - '?';
result |= ((b & 31) << shift);
shift += 5;
if (b < 32)
break;
}
lng += ((result & 1) != 0 ? ~(result >> 1) : result >> 1);
/* Add the new Lat/Lng to the Array. */
out[cnt++] = new GeoPoint((lat*10),(lng*10));
}
return out;
}catch(Exception e) {
e.printStackTrace();
}
return out;
}
public static int[] decodeZoomLevels(String encodedZoomLevels){
int[] out = new int[encodedZoomLevels.length()];
int index = 0;
for(char c : encodedZoomLevels.toCharArray())
out[index++] = c - '?';
return out;
}
}
Google MapsAndroidAPIユーティリティライブラリを使用できます。PolyUtil.decode(String encoded)
それはあなたが必要とすることを正確に行う方法を提案します!
GeoPointが機能しません。それを使用するライブラリが見つかりません。代わりにLatLng値を返す関数を次に示します。
public static ArrayList<LatLng> decodePolyPoints(String encodedPath){
int len = encodedPath.length();
final ArrayList<LatLng> path = new ArrayList<LatLng>();
int index = 0;
int lat = 0;
int lng = 0;
while (index < len) {
int result = 1;
int shift = 0;
int b;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
result = 1;
shift = 0;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
path.add(new LatLng(lat * 1e-5, lng * 1e-5));
}
return path;
}
Google MapsAndroidAPIユーティリティライブラリから取得しました。コードはここにあります
コード内のハードコードされた文字列でこれをテストするときのいくつかの注意点、Javaは正しく読み取ることができません
"\"
Javaで正しく読み取られるようにするには、別のスラッシュを追加する必要があります。
"\\"
エンコードされた文字列には奇妙な文字が含まれているため、注意が必要です。
以下のコードは、エンコードされたポリラインをデコードします
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b=0, shift = 0, result = 0;
do {
if(index<len) {
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 {
if(index<len) {
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;
}