gpt4 book ai didi

java - 在 Android map 上获取多个图钉

转载 作者:行者123 更新时间:2023-12-01 14:40:19 25 4
gpt4 key购买 nike

我正在尝试使用 Google Maps Android API v2 在我的 map 上获取多个图钉我已从 SQlite DB 中获取了纬度/经度值现在,当我在 map 上添加图钉时,我陷入了困境(出现解析错误 - 无法将此 51.4738 字符串解析为 Int。)

我的代码是这样的:

public class MainActivity extends FragmentActivity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);

try {

myDbHelper.createDataBase();

} catch (IOException ioe) {

throw new Error("Unable to create database");

}

try {

myDbHelper.openDataBase();


Cursor cursor = myDbHelper.getAllContacts();

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();

int nameIndex = cursor.getColumnIndexOrThrow("name");
int latIndex = cursor.getColumnIndexOrThrow("latitude");
int lngIndex = cursor.getColumnIndexOrThrow("longitude");
String[] result_lats = new String [cursor.getCount()];
String[] result_lngs = new String [cursor.getCount()];
String[] result_names = new String [cursor.getCount()];
while(cursor.moveToNext()){


String lats = cursor.getString(latIndex);
String lngs = cursor.getString(lngIndex);
String nme = cursor.getString(nameIndex);

// Double lat = cursor.getFloat(latIndex) * 1E6;
// Double lnt = cursor.getFloat(lngIndex) * 1E6;

result_lats [cursor.getPosition()] = String.valueOf(lats);

// tried this also :
// result_lats [cursor.getPosition()] = String.valueOf(lat);
// use lat.intValue() when adding on marker
// -- Output(Using String.valueOf(lat.intValue()))- 10 Greek Street 51514098/-130659
// -- Output(Using String.valueOf(lat)) - 10 Greek Street 5.1514098E7/-130659.0
result_lngs [cursor.getPosition()] = String.valueOf(lngs);
result_names [cursor.getPosition()] = nme;

}

Log.d("Latitude22 :", String.valueOf(result_lats[2])); // `Output Lat -> 51.5195`
Log.d("Longitude22", String.valueOf(result_lngs[2]));
Output Long -> -0.126092

// Integer Longitu = Integer.valueOf(lats[1]);

// Integer Latitu = Integer.valueOf(result_lngs[1]);

mMap.addMarker(new MarkerOptions().position(new LatLng(51.156685, -0.165324)).title(result_names[1]));


for(int i=0 ; i<result_lats.length ; i++){
Log.d("Names are :", String.valueOf(result_names[i]));
Log.d("Latitudes are :", String.valueOf(result_lats[i]));
Log.d("Longitudes are :", String.valueOf(result_lngs[i]));

What should my code be to be used here so that i can get all pins shown on Map
i knw it will be somewhat like this :

// mMap.addMarker(new MarkerOptions().position(new LatLng(result_lats[i], result_lngs[i]).title(result_names[i]));

// but the format is likely this : LatLng(53.558, 9.927); and here i have String value result_lats[i]

}


cursor.close();
// ---- End Cursor coding
}catch(SQLException sqle){

throw sqle;

}
}

请谁能帮我解决这个问题......

现在已经解决了,我只需要这样做:

for(int i=0 ; i<result_lats.length ; i++){
Log.d("Names are :", String.valueOf(result_names[i]));
Log.d("Latitudes are :", String.valueOf(result_lats[i]));
Log.d("Longitudes are :", String.valueOf(result_lngs[i]));

Double lat2 = Double.valueOf(result_lats[i]);
Double lng2 = Double.valueOf(result_lngs[i]);
mMap.addMarker(new MarkerOptions().position(new LatLng(lat2,lng2)).title(result_names[i]));


}

最佳答案

你需要像这样解析

Double lat = Double.valueOf(lats[1]);

您无法将双字符串值解析为可验证的Integer

对于 map 上的多个图钉,这可能会帮助您[这里需要传递经纬度数组]

private void DisplayAllMarkersonMap(GoogleSetGetArray googleSetGetArray) {
try
{
GeoPoint point = null;
for(int i=0;i<googleSetGetArray.getGoogleSetGets().size();i++)
{
//Drawable drawable = new BitmapDrawable(CheckIn.bitmaps.get(i)); //Drawables that are coming from google api
point = new GeoPoint((int) ((googleSetGetArray.getGoogleSetGets().get(i).getLat() * 1E6)),(int)((googleSetGetArray.getGoogleSetGets().get(i).getLng()) * 1E6));
overlayitem = new OverlayItem(point,googleSetGetArray.getGoogleSetGets().get(i).getName(),
googleSetGetArray.getGoogleSetGets().get(i).getVicinity());
itemizedoverlay = new CheckInMapItemizedOverlay(drawable,mapView,true,googleSetGetArray.getGoogleSetGets().get(i).getId());
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
mapController.animateTo(point);

}catch(Exception ex)
{
ex.printStackTrace();
}
}

关于java - 在 Android map 上获取多个图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16031411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com