我实际上已经编写了一个代码,用于在 android 中按用户显示谷歌地图。用户在编辑文本中输入我在 JSON 中用于获取纬度和经度的位置,并将地图指向该纬度和经度。现在我面临的问题是我已经将用户输入的位置 dat 存储在一个名为 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();
}