尝试从我的SQLite数据库中检索经度和纬度值,并在谷歌地图上放置标记

时间:2017-04-28 15:17:49

标签: android sqlite dictionary markers

我将经度和经度值存储在SQLite数据库中,并且还有一个谷歌地图。我每次添加坐标时都试图在地图上显示自定义标记。我觉得我存储的价值并不遥远,我可以在地图上放置硬编码的自定义标记。我有一个for循环,它应循环遍历数据库中的每个条目。任何帮助将不胜感激。

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    SQLiteDatabase db;

    private static final String DATABASE_NAME = "heartwise";
    private static final int DATABASE_VERSION = 4;
    private static final String TABLE_SERVICE = "service";
    private static final String COLUMN_ID = "ID";
    private static final String COLUMN_LOCATION = "location";
    private static final String COLUMN_CONDITION = "condition";
    private static final String COLUMN_DATE = "date";
    private static final String COLUMN_TIME = "time";
    private static final String COLUMN_LATITUDE = "latitude";
    private static final String COLUMN_LONGITUDE = "longitude";


    private static final String CREATE_SERVICE_TABLE = "create table if not 
    exists "
                + TABLE_SERVICE + "("
                + COLUMN_ID + " TEXT PRIMARY KEY,"
                + COLUMN_LOCATION + " TEXT,"
                + COLUMN_CONDITION + " TEXT,"
                + COLUMN_DATE + " TEXT,"
                + COLUMN_TIME + " TEXT,"
                + COLUMN_LATITUDE + " DECIMAL,"
                + COLUMN_LONGITUDE + " DECIMAL);";

    Context context;
    public DatabaseHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
         this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       this.db = db;
       db.execSQL(CREATE_SERVICE_TABLE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
       db.execSQL("DROP TABLE IF EXISTS " + TABLE_SERVICE);
       // Create tables again
       onCreate(db);
    }

   public void insertIntoServiceTable(Service service)
   {
       db = this.getWritableDatabase();
       ContentValues values = new ContentValues();

       String query = "SELECT * FROM service";
       db.rawQuery(query, null);

       values.put(COLUMN_ID, service.getID());
       values.put(COLUMN_LOCATION, service.getLocation());
       values.put(COLUMN_CONDITION, service.getCondition());
       values.put(COLUMN_DATE, service.getDate());
       values.put(COLUMN_TIME, service.getTime());
       values.put(COLUMN_LATITUDE, service.getLatitude());
       values.put(COLUMN_LONGITUDE, service.getLongitude());

       db.insert(TABLE_SERVICE, null, values);
       db.close();
   }  

   public List<Service> getDataFromServiceTable()
   {
       List<Service> serviceList = new ArrayList<Service>();    

       String query = "select * from "+ TABLE_SERVICE;
       db = this.getWritableDatabase();
       Cursor cursor = db.rawQuery(query,null);

       if (cursor.moveToFirst()){
        do {
            Service service = new Service();
            service.setID(cursor.getString(0));
            service.setLocation(cursor.getString(1));
            service.setCondition(cursor.getString(2));
            service.setDate(cursor.getString(3));
            service.setTime(cursor.getString(4));
            service.setLatitude(cursor.getString(5));
            service.setLongitude(cursor.getString(6));
            serviceList.add(service);

        }while (cursor.moveToNext());
      }
      return serviceList;
    }

    public void updateServiceData(Service service)
    {
       db = this.getWritableDatabase();
       ContentValues values = new ContentValues();

       String query = "SELECT * FROM service";
       db.rawQuery(query, null);

       values.put(COLUMN_ID, service.getID());
       values.put(COLUMN_LOCATION, service.getLocation());
       values.put(COLUMN_CONDITION, service.getCondition());
       values.put(COLUMN_DATE, service.getDate());
       values.put(COLUMN_TIME, service.getTime());
       values.put(COLUMN_LATITUDE, service.getLatitude());
       values.put(COLUMN_LONGITUDE, service.getLongitude());

       db.update(TABLE_SERVICE, values, COLUMN_ID + "=?", new String[]
      {String.valueOf(service.getID())});
     }

   public void deleteItem(String id){
       db = this.getWritableDatabase();
       db.delete(TABLE_SERVICE,COLUMN_ID + "=?",new String[]
    {String.valueOf(id)});
       db.close();
      }
  }

AddScreen.java

 public class AddScreen extends AppCompatActivity
 {
     EditText editTextID, editTextLocation, editTextDate, editTextTime, 
     editTextLatitude, editTextLongitude;
     RadioGroup radioGroup;
     Button buttonSubmit;
     DatabaseHelper helper;        //instance of DatabaseHelper class
     List<Service> serviceList;

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

     serviceList = new ArrayList<Service>();   //

     editTextID = (EditText) findViewById(R.id.idET);
     editTextLocation = (EditText) findViewById(R.id.locationET);
     radioGroup =(RadioGroup)findViewById(R.id.radio_group);
     editTextDate = (EditText) findViewById(R.id.dateET);
     editTextTime = (EditText) findViewById(R.id.timeET);
     editTextLatitude = (EditText) findViewById(R.id.latitudeET);
     editTextLongitude = (EditText) findViewById(R.id.longitudeET);
     buttonSubmit = (Button) findViewById(R.id.submitButton);

     buttonSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            String id = editTextID.getText().toString();
            String location = editTextLocation.getText().toString();
            String radioValue =((RadioButton)findViewById(radioGroup.
            getCheckedRadioButtonId())).getText().toString();
            String date = editTextDate.getText().toString();
            String time = editTextTime.getText().toString();
            String latitude = editTextLatitude.getText().toString();
            String longitude = editTextLongitude.getText().toString();

            if(id.length() == 0)
            {
                editTextID.requestFocus();
                editTextID.setError("Please enter the AED ID");
            }
            else if(id.length() > 5)
            {
                editTextID.requestFocus();
                editTextID.setError("The ID must be less than 6 
                characters");
            }
            else if(location.length() == 0)
            {
                editTextLocation.requestFocus();
                editTextLocation.setError("Please enter the AED location");
            }
            else if(date.length() == 0)
            {
                editTextDate.requestFocus();
                editTextDate.setError("Please enter the date");
            }
            else if(time.length() == 0)
            {
                editTextTime.requestFocus();
                editTextTime.setError("Please enter the time");
            }
            else if(latitude.length() == 0)
            {
                editTextLatitude.requestFocus();
                editTextLatitude.setError("Please enter location latitude");
            }
            else if(longitude.length() == 0)
            {
                editTextLongitude.requestFocus();
                editTextLongitude.setError("Please enter location 
                longitude");
            }
            else
            {
                Service service = new Service();  
                service.setID(id);
                service.setLocation(location);
                service.setCondition(radioValue);
                service.setDate(date);
                service.setTime(time);
                service.setLatitude(latitude);
                service.setLongitude(longitude);

                helper = new DatabaseHelper(AddScreen.this);
                helper.insertIntoServiceTable(service);
            }
            editTextID.setText("");
            editTextLocation.setText("");
            editTextDate.setText("");
            editTextTime.setText("");
            editTextLatitude.setText("");
            editTextLongitude.setText("");
            Toast.makeText(AddScreen.this, "You have added service details 
            successfully!", Toast.LENGTH_LONG);
         }
      });
   }

SearchScreen.xml(地图)

  public class SearchScreen extends FragmentActivity implements 
  OnMapReadyCallback, GoogleMap.OnMarkerClickListener {

  private GoogleMap googleMap;


  public static final List<LatLng> locations = new ArrayList<>();

  EditText editTextLongitude;
  EditText editTextLatitude;

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

     SupportMapFragment mapFragment = (SupportMapFragment) 
     getSupportFragmentManager().findFragmentById(map);
     mapFragment.getMapAsync(this);;

     editTextLatitude = (EditText)findViewById(R.id.latitudeET);
     editTextLongitude = (EditText)findViewById(R.id.longitudeET);
  }

  @Override
  public void onMapReady(GoogleMap googleMap) {

  this.googleMap = googleMap;
  this.googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

  for (LatLng location: locations){

        this.googleMap.addMarker(new MarkerOptions()
            .position(location)
            .title("bla bla bla")
            .snippet("Contact No: 051 27385")
            .icon(BitmapDescriptorFactory.fromResource
            (R.drawable.heartmarker)));
       }

       this.googleMap.setOnMarkerClickListener(this);
   }

   public void onMapSearch(View view)
   {
       EditText locationSearch = (EditText) 
       findViewById(R.id.editTextSearch);
       String location = locationSearch.getText().toString();
       List<Address> addressList = null;

       if (location != null || !location.equals("")) {
            Geocoder geocoder = new Geocoder(this);
            try {
                addressList = geocoder.getFromLocationName(location, 1);

            } catch (IOException e) {
               e.printStackTrace();
            }
        Address address = addressList.get(0);
        LatLng latLng = new LatLng(address.getLatitude(), 
        address.getLongitude());
        googleMap.addMarker(new 
        MarkerOptions().position(latLng).title("Marker"));
        googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    }
}

0 个答案:

没有答案