谁能告诉我如何使用 android studio 在 android 数据库中存储地理编码器地址?
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
但不能存储地址部分。
只需 return 您的地址作为字符串,并使用 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;
}
我能够看到从 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
但不能存储地址部分。
只需 return 您的地址作为字符串,并使用 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;
}