1
我有一個簡單的谷歌地圖實現,我用我的strings.xml中的雙精度填充兩個類型數組,所以我可以使用它們來填充經度和緯度。類型數組在getTypedArray調用後爲空
這裏是方法:
@Override
public void onMapReady(GoogleMap map) {
map.setMyLocationEnabled(true);
TypedArray lats = getResources().obtainTypedArray(R.array.Lats);
TypedArray longs = getResources().obtainTypedArray(R.array.Longs);
int length = longs.getIndexCount();
Log.e("MY APP LOG", "here");
Log.e("MY APP LOG", "length = " + length);
for(int i = 0; i < length; i++)
{
LatLng coord = new LatLng(lats.getFloat(i,0),longs.getFloat(i,0));
Log.e("MY APP LOG", "here2");
Log.e("MY APP LOG", "latitude = " + lats.getFloat(i,0));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(coord));
}
LatLng coord = new LatLng(lats.getFloat(0,0),longs.getFloat(0,0));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(coord, 13));
}
我的strings.xml:
<array name="Lats">
<item>-33.867</item>
<item>-33.869</item>
<item>-33.871</item>
<item>-33.873</item>
<item>-33.876</item>
<item>-33.881</item>
<item>-33.883</item>
<item>-33.885</item>
<item>-33.891</item>
</array>
<array name="Longs">
<item>151.206</item>
<item>151.209</item>
<item>151.211</item>
<item>151.205</item>
<item>151.204</item>
<item>151.207</item>
<item>151.203</item>
<item>151.201</item>
<item>151.200</item>
</array>
的logcat中顯示:
04-30 05:00:00.727 24986-24986/com.example.andrew.ubair4 E/MY APP LOG﹕ here
04-30 05:00:00.727 24986-24986/com.example.andrew.ubair4 E/MY APP LOG﹕ length = 0
我想你必須嘗試整數數組:http://developer.android.com/guide/topics/resources/more-resources.html#IntegerArray –
@HareshChhelana它會攜帶小數? –
然後嘗試在String-Array中將其定義爲String,並在使用時將其轉換爲double。 –