我想将我的位置保存在数据库中, 当使用Jason解析器时,此错误出现在(String name = latlng.getText()。toString();)
(方法getText()未定义类型LatLng)其中LatLng来自LatLng类型
任何人都可以解决它?
protected String doInBackground(String... args) {
String name = latlng.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("yout_location", name));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(add_location,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), NamesActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
答案 0 :(得分:1)
我参与过类似的项目。
如果您尝试存储位置名称,则需要使用地理编码器将LatLng点转换为“formatted_address”。
如果您尝试将数据库中的纬度和经度值存储为“双”数据类型,则需要将它们转换为字符串,如下所示
String lat = Double.toString(latlng.latitude);
String lng = Double.toString(latlng.longitude);
<强>更新强>
将LatLng点传递给asynctask并用此
替换您的代码protected Void doInBackground(LatLng... params) {
Log.d("Maps", "background");
String lat = Double.toString(params[0].latitude);
String lng = Double.toString(params[0].longitude);
String strUrl = "http://yourdomain.com/save.php";
URL url = null;
try {
url = new URL(strUrl);
Log.d("Maps", "url fetch");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
Log.d("Maps", "opened");
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
connection.getOutputStream());
outputStreamWriter.write("lat=" + lat + "&lng="+lng);
Log.d("Maps", "wrote");
outputStreamWriter.flush();
outputStreamWriter.close();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( (line = reader.readLine()) != null){
sb.append(line);
}
reader.close();
iStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
答案 1 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
Geocoder geocoder = new Geocoder(context);
List<Address> list = null;
try {
list = geocoder.getFromLocation(latlng.latitude, latlng.longitude, 1);
} catch (Throwable e) {
e.printStackTrace();
}
String name = list.get(0).getSubAdminArea();
答案 2 :(得分:0)
我认为你想使用纬度和经度得到这个位置。你可以使用以下方法做到这一点。
public String getAddress(Context ctx, double latitude, double longitude) {
StringBuilder result = new StringBuilder();
try {
Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
List<address>
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String locality=address.getLocality();
String city=address.getCountryName();
String region_code=address.getCountryCode();
String zipcode=address.getPostalCode();
double lat =address.getLatitude();
double lon= address.getLongitude();
result.append(locality+" ");
result.append(city+" "+ region_code+" ");
result.append(zipcode);
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
return result.toString();
}
要从 LatLng 对象获取纬度和经度,您可以执行以下操作:
double lat = latlng.latitude;
double lng = latlng.longitude;
然后只需在 asynctask 中调用方法getAddress(),如下所示:
private class GetCurrentPlace extends AsyncTask<string string="" void=""> {
double lat;
double long;
GetCurrentPlace(double lat,double long)
{
this.lat=lat;
this.long=long;
}
@Override
protected String doInBackground(String... urls) {
String address= getAddress(context, lat, long);
return address;
}
@Override
protected void onPostExecute(String resultString) {
dialog.dismiss();
result.setText(resultString);
}
}
将asynctask称为:
new GetCurrentPlace(latlng.latitude,latlng.longitude).execute();