嗨亲爱的我正在从谷歌地图搜索位置并获取它。现在我想保存它以便我可以通过从数据库获取再次使用它。现在告诉我如何保存它以及如何从LatLang表单中的数据库中检索它 这是我从地图搜索位置的代码。
LatLng latLng;
btn_find.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Getting user input location
String location = etLocation.getText().toString();
if(location!=null && !location.equals("")){
new GeocoderTask().execute(location);
}
}
});

private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
@Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
googleMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
googleMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
&#13;
现在请告诉我如何通过保存按钮保存此位置?
答案 0 :(得分:0)
我认为您可以使用SharedPrefrence,如下所示
声明SharedPreferences settings;
settings = context.getSharedPreferences("myprefs", 0); //global declaration in oncreate
在同步任务的onPost中
@覆盖 protected void onPostExecute(List addresses){
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
googleMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
SharedPreferences.Editor editor = settings.edit();
editor.putString(&#34; latitude&#34;,Double.toString(address.getLatitude()。toString()));&#39; editor.putString(&#34;经度&#34;,Double.toString(address.getLongitude())); editor.commit();
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
googleMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
获取值
String latitude=settings.getString("latitude","0");
String longitude=settings.getString("longitude","0");
double lati = Double.parseDouble(lat);
double lngi = Double.parseDouble(lng);
LatLng LOCATION =新的LatLng(lati,lngi);
答案 1 :(得分:0)
To save in the database;
DBLocation helper1=new DBLocation(getBaseContext());
database = helper1.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ONLINELOCATION_lATITUDE, latitude);
values.put(ONLINELOCATION_LONGITUDE, longitude);
Log.i("Insert Into Database","Insert");
database.insert(ONLINELOCATION_TABLE, null, values);
要检索:
Cursor callpending_cursor=null;
database=helper1.getReadableDatabase();
callpending_cursor=database.query(ONLINELOCATION_TABLE, new String[] {ONLINELOCATION_ID,ONLINELOCATION_AID_ID,ONLINELOCATION_USERID_ID,ONLINELOCATION_lATITUDE,ONLINELOCATION_LONGITUDE,ONLINELOCATION_ID +" DESC"}, null, null, null, null, null);
Log.i("step8","step8");
//callpending_cursor.moveToLast();
if(!callpending_cursor.isAfterLast()) {
callpending_cursor.moveToFirst();
do{
Log.i("step9","step9");
String callpending_id=callpending_cursor.getString(0);
Aid_ID=callpending_cursor.getString(1);
USER_Id=callpending_cursor.getString(2);
USER_Latitude=callpending_cursor.getString(3);
USER_Longitude=callpending_cursor.getString(4);
callpending_cursor.moveToNext();