在地图Android上获取多个引脚

时间:2013-04-16 07:44:31

标签: java android google-maps

我正在尝试使用Google Maps Android API v2在地图上获取多个图钉 我从SQlite DB中获取了Lat / Long的值 现在我在地图上添加引脚时遇到困难(获取解析错误 - 无法将此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]));


      }

1 个答案:

答案 0 :(得分:1)

你需要解析像

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

您无法将double String值解析为Integer veriable;

对于地图上的多个引脚,这可能对您有所帮助[这里需要通过lat&amp; amp;长阵]

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();
                }
        }