我已经编写了用于在地图上显示各种人的位置的代码。我正在从 PHP 脚本解析 Json 数据。我想在地图上显示带有文字的图标。我的图标显示得很好,但没有显示相关的文本。我的代码如下。如何在地图上显示带有 INFO 的图标?
public class Maps extends MapActivity {
private void callPolice()
{
Intent callintent = new Intent(Intent.ACTION_CALL);
callintent.setData(Uri.parse("tel:6100"));
startActivity(callintent);
}
private MapView mapView;
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//button code starts here
Button panic_button = (Button) findViewById(R.id.button1);
panic_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xyz.com/index.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("uid", "4"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
//Toast.makeText(this, resId, duration)
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
callPolice();
}
});
//button code ends
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mapView = (MapView) findViewById(R.id.themap);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
mapView.invalidate();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xyz/maps.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
double LAT;
double LANG;
String INFO;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
LAT=json_data.getDouble("lat");
LANG=json_data.getDouble("lon");
INFO=json_data.getString("name");
//Overlay code
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
// CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
AddItemizedOverlay itemizedOverlay = new AddItemizedOverlay(drawable, this);
//GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
GeoPoint point = new GeoPoint((int)(LAT * 1e6),(int)(LANG * 1e6));
OverlayItem overlayitem = new OverlayItem(point, INFO, INFO);
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
MapController mapController = mapView.getController();
mapController.animateTo(point);
mapController.setZoom(15);
//Overlay code
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_maps, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_friends:
Intent intent = new Intent(this, Friends.class);
startActivity(intent);
return true;
case R.id.menu_invite:
Intent intent1 = new Intent(this, Invite.class);
startActivity(intent1);
return true;
case R.id.menu_logout:
Intent intent2 = new Intent(this, Logout.class);
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}