![](/img/trans.png)
[英]Can Anyone Tell Me Why This Text Is Not Centering In Android Studio?
[英]can anyone tell me how to store geocoder address in android database using android studio?
我可以看到從GeoCoder
代碼獲取的地址(用於單擊偵聽器),並且可以在日志文件中顯示,但無法將該地址存儲在本地DataBase
。
誰能幫我解決這個問題。
提前致謝。
package com.example.raghotham.androidgeocodelocation;
import java.util.List;
public class MainActivity extends Activity {
Button btnGPSShowLocation;
Button btnShowAddress;
TextView tvAddress;
final DatabaseHandler1 db = new DatabaseHandler1(this);
AppLocationService appLocationService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvAddress = (TextView) findViewById(R.id.tvAddress);
appLocationService = new AppLocationService(
MainActivity.this);
btnGPSShowLocation = (Button) findViewById(R.id.btnGPSShowLocation);
btnGPSShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Location gpsLocation = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation != null) {
double latitude = gpsLocation.getLatitude();
double longitude = gpsLocation.getLongitude();
String result = "Latitude: " + gpsLocation.getLatitude() +
" Longitude: " + gpsLocation.getLongitude();
tvAddress.setText(result);
} else {
showSettingsAlert();
}
}
});
btnShowAddress = (Button) findViewById(R.id.btnShowAddress);
btnShowAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Location location = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LocationAddress locationAddress = new LocationAddress();
locationAddress.getAddressFromLocation(latitude, longitude,
getApplicationContext(), new GeocoderHandler());
} else {
showSettingsAlert();
}
}
});
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MainActivity.this);
alertDialog.setTitle("SETTINGS");
alertDialog.setMessage("Enable Location Provider! Go to settings menu?");
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
MainActivity.this.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
private class GeocoderHandler extends Handler {
@Override
public void handleMessage(Message message) {
String locationAddress;
switch (message.what) {
case 1:
Bundle bundle = message.getData();
locationAddress = bundle.getString("address");
Log.d("testing: ", locationAddress);
break;
default:
locationAddress = null;
}
tvAddress.setText(locationAddress);
}
}
}
我能夠存儲latitude and longitude
但不能存儲地址部分。
只需以String形式返回您的地址,然后使用SQlite將其存儲在本地數據庫中即可。 在SQLite中存儲字符串很容易。 下面是獲取地址的代碼。
public static String getAddressFromLocation(Context context,
double latitude, double longitude) {
String address = "";
Geocoder geocoder;
List<Address> listAddresses;
try {
geocoder = new Geocoder(context, Locale.getDefault());
if (isNetworkAvailable(context)) {
listAddresses = geocoder
.getFromLocation(latitude, longitude, 1);
for (Address addrss : listAddresses) {
String adminArea = addrss.getAdminArea();
Logging(SHOW_LOG, TAG, "adminArea = " + adminArea);
String locality = addrss.getLocality();
Logging(SHOW_LOG, TAG, "locality = " + locality);
String postalCode = addrss.getPostalCode();
Logging(SHOW_LOG, TAG, "postalCode = " + postalCode);
String address1 = addrss.getAddressLine(0);
Logging(SHOW_LOG, TAG, "address1 = " + address1);
String cityState = addrss.getAddressLine(1);
Logging(SHOW_LOG, TAG, "cityState = " + cityState);
String countryName = addrss.getCountryName();
Logging(SHOW_LOG, TAG, "countryName = " + countryName);
}
address = listAddresses.get(0).getAddressLine(0) + " "
+ listAddresses.get(0).getAddressLine(1) + " "
+ listAddresses.get(0).getCountryName();
} else {
address = "";
}
} catch (Exception e) {
address = "";
}
Log.i(TAG, "address : " + address);
return address;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.