可能重复:
how to get the name entered by the user and search that respective location in edittext
我实际上已经根据android中的用户编写了用于显示谷歌地图的代码。用户在编辑文本中输入位置,我在JSON中用于获取纬度和经度,并将地图指向该纬度和经度。现在我面临的问题是我已经存储了位置数据用户输入名为location的变量,并且我已经在JSON中传递了dat变量。但每当我运行程序并进入任何地点时,地图都指向海得拉巴附近 我已经在tabview中实现了这段代码 这是我的代码......
这里的基本概率是来自json的纬度和经度未被传递到其他页面......
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
Et = (EditText) findViewById(R.id.edt);
Bt = (Button) findViewById(R.id.Search);
location = Et.getText().toString();
final String readTwitterFeed = readTwi("http://maps.googleapis.com/maps/api/geocode/json?address=+location+&sensor=false");
Bt.setOnClickListener(new OnClickListener()
{
String strUrl,lon;
public void onClick(View v)
{
try
{
JSONObject jsonObject =new JSONObject(readTwitterFeed).getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
strUrl=jsonObject.getString("lat");
lon=jsonObject.getString("lng");
Log.i("location",strUrl);
Log.i("longtin",lon);
}
catch (Exception e)
{
e.printStackTrace();
}
Intent intent=new Intent(MapView.this,MapsActivity.class);
intent.putExtra("lat",strUrl);
intent.putExtra("lng",lon);
startActivity(intent);
}
});
}
private String readTwi(String url)
{
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(MapView.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}