0

我在 MYSQL DB 中有一组 lat 和 lon 值。我使用 JSON(从 PHP 代码中检索)在 android 应用程序中检索了它,并且能够在地图上显示这些位置。但我不知道如何在这些位置上放置标记。请帮我。

以下是我的代码:

try {

    HttpClient client = new DefaultHttpClient();
    URI website = new URI("http://192.168.1.15/latlonret1.php");
    HttpGet request = new HttpGet();
    request.setURI(website);
    HttpResponse response = client.execute(request);
    in = new BufferedReader(new InputStreamReader(response
            .getEntity().getContent()));
    StringBuffer sb = new StringBuffer("");
    String l = "";
    String nl = System.getProperty("line.separator");

    while ((l = in.readLine()) != null) {
        sb.append(l + nl);
    }
    in.close();
    data = sb.toString();
    // return data;
} catch (Exception e) {
    Log.e("log_tag", "Error converting result " + e.toString());
}
    try {

        JSONArray jArray = new JSONArray(data);


        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            JSONObject json_data1 = jArray.getJSONObject(i);


            returnString =json_data.getString("lat") + "\n";
            returnString1 =json_data1.getString("lon") + "\n";
            System.out.println(returnString);
            System.out.println(returnString1);
      }
 Intent viewIntent =new Intent(Androidmap.this,Mapview.class);

 Bundle bundle = new Bundle();
 bundle.putString("stuff", returnString); 
 bundle.putString("stuff1", returnString1); 
 viewIntent.putExtras(bundle);

 startActivity(viewIntent);
    }
4

3 回答 3

1

使用 ItemizedOverlays 在地图上绘制点。这是 Lars Vogel 教程的链接

http://www.vogella.com/articles/AndroidGoogleMaps/article.html#maps_overlays

于 2012-12-18T07:17:39.253 回答
0

Use Google map v2. V1 been deprecated and key been no longer used.

于 2013-04-18T04:13:31.567 回答
0

从您从谷歌获得的 JSON 中检索位置(lat.long)后

按着这些次序

  1. 位置 l=新位置(LocationManager.GPS_PROVIDER);
  2. 位置点标记=新位置(l);
  3. 然后,您可以设置点的标题、邻近度和 point_marker 的其他详细信息。bY 制作一个像这样的捆绑包。
  4. 捆绑EXTRAS =新捆绑();
  5. EXTRAS.putString("TITLE",TITLE); EXTRAS.putString("邻近地区", 邻近地区); poi.setExtras(EXTRAS);
  6. 因此,现在您的 ArrayList 中充满了位置对象,这些对象本身就充满了位置详细信息。
  7. 现在将这些添加到地图中
  8. 逐一检索位置对象,并为每个位置创建一个 GEOPOINT 并将其标记在地图上,如下所示

    for(Location l:locs){ Log.d("PAIRED-VALUES:",String.valueOf(l.getLatitude())+","+String.valueOf(l.getLongitude())); GeoPoint 点 =new GeoPoint((int)(l.getLatitude()*1E6), (int)(l.getLongitude()*1E6));

        controller.animateTo(point);
        controller.setZoom(17);
        Map_OverLays=map.getOverlays();
    
    
    
        itemizedoverlay=new HelloItemizedOverlay(draw, context);
        overlayitem = new OverlayItem(point,l.getExtras().getString("TITLE"),l.getExtras().getString("VICINITY"));
        itemizedoverlay.addOverlay(overlayitem);
        Map_OverLays.add(itemizedoverlay);
    
        map.invalidate();
    

    }

这里是逐项叠加类

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;


@SuppressWarnings("rawtypes")
public class HelloItemizedOverlay extends ItemizedOverlay {

    private ArrayList<OverlayItem> Map_Overlays=new ArrayList<OverlayItem>();
    //private ArrayList<OverlayItem> Map_Overlays1=new ArrayList<OverlayItem>();        
    Context mcontext;
    public HelloItemizedOverlay(Drawable default_marker) {
        super(boundCenterBottom(default_marker));
        // TODO Auto-generated constructor stub
    }
    public HelloItemizedOverlay(Drawable default_marker,Context context) {
        super(boundCenterBottom(default_marker));
        mcontext=context;       
        //populate();
        // TODO Auto-generated constructor stub
    }
@Override
    protected boolean onTap(int arg0) {
    OverlayItem item=Map_Overlays.get(arg0);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mcontext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;

    }   
public void addOverlay(OverlayItem item)
{
    Log.d("NumberOfTimes" , item.getSnippet());
    Map_Overlays.add(item);
    populate();

}
    @Override
    protected OverlayItem createItem(int arg0) {
        // TODO Auto-generated method stub
        return Map_Overlays.get(arg0);

    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return Map_Overlays.size();
    }

}
于 2013-04-18T05:46:07.610 回答