2012-08-30 67 views
0

我已經編寫了顯示地圖上各種人物位置的代碼。我從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); 
    } 
} 
} 

回答

0

修改您的AddItemizedOverlay以包含的onDraw方法倍率,同時修改你的構造,包括TEXTSIZE參數:

public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem> 
{ 
//member variables 
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
private Context mContext; 
private int mTextSize; 


public MapItemizedOverlay(Drawable defaultMarker, Context context, int textSize) 
{ 
    super(boundCenterBottom(defaultMarker)); 
    mContext = context; 
    mTextSize = textSize; 
} 


//In order for the populate() method to read each OverlayItem, it will make a request to createItem(int) 
// define this method to properly read from our ArrayList 
@Override 
protected OverlayItem createItem(int i) 
{ 
    return mOverlays.get(i); 
} 


@Override 
public int size() 
{ 
    return mOverlays.size(); 
} 

@Override 
protected boolean onTap(int index) 
{ 
    OverlayItem item = mOverlays.get(index); 

    //Do stuff here when you tap, i.e. : 
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
    dialog.setTitle(item.getTitle()); 
    dialog.setMessage(item.getSnippet()); 
    dialog.show(); 

    //return true to indicate we've taken care of it 
    return true; 
} 

@Override 
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) 
{ 
    super.draw(canvas, mapView, shadow); 

    if (shadow == false) 
    { 
     //cycle through all overlays 
     for (int index = 0; index < mOverlays.size(); index++) 
     { 
      OverlayItem item = mOverlays.get(index); 

      // Converts lat/lng-Point to coordinates on the screen 
      GeoPoint point = item.getPoint(); 
      Point ptScreenCoord = new Point() ; 
      mapView.getProjection().toPixels(point, ptScreenCoord); 

      //Paint 
      Paint paint = new Paint(); 
      paint.setTextAlign(Paint.Align.CENTER); 
      paint.setTextSize(mTextSize); 
      paint.setARGB(150, 0, 0, 0); // alpha, r, g, b (Black, semi see-through) 

      //show text to the right of the icon 
      canvas.drawText(item.getTitle(), ptScreenCoord.x, ptScreenCoord.y+mTextSize, paint); 
     } 
    } 
} 


public void addOverlay(OverlayItem overlay) 
{ 
    mOverlays.add(overlay); 
    populate(); 
} 


public void removeOverlay(OverlayItem overlay) 
{ 
    mOverlays.remove(overlay); 
    populate(); 
} 


public void clear() 
{ 
    mOverlays.clear(); 
    populate(); 
} 

} 
+0

thxx Vishwa taht工作就像魅力 – imran

+0

如何添加的看到地址的設備在警示框上點擊。 – imran