由于以下原因,无法将Geopoints添加到谷歌地图中;无法获得连接工厂客户端

时间:2013-04-09 00:00:53

标签: android google-maps mapactivity mylocationoverlay

我在尝试将多个Geopoint位置绘制到Google地图上时收到此错误。我正在通过PHP和JSON从MySQL数据库中读取纬度和经度。我查看了主要示例,例如Couldn't get connection factory clientCouldn't get connection factory client - fighting with Google MapsAndroid MapActivity : Couldn't get connection factory client。我可以确认它不是无效的API密钥,因为我生成了2个不同的密钥,返回了同样的错误。它也不是API级别的问题,我试图在API级别17和级别8上运行应用程序,但仍然没有乐趣。当我运行代码时,它显示地图没有问题然后转到我的异常Toast消息“错误显示内容”
这是我的mapview.java代码:

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class mapview extends MapActivity 
{

//protected MapView mapview;

JSONArray jarray;
String result=null;
InputStream is=null;
StringBuilder sb=null;

double LAT;
double LNG;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);

    MapView mapview = (MapView) findViewById(R.id.mapview);
    mapview.setBuiltInZoomControls(true);

    //ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    try 
    {

         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("http://10.0.2.2/displaymarkers.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());
    }


        try{        


        jarray = new JSONArray("$json_output[]");
        JSONObject json_data = null;
                for(int i = 0; i < jarray.length(); i++)
                {
                    json_data = jarray.getJSONObject(i);

                    LAT=json_data.getDouble("lat");
                    LNG=json_data.getDouble("lng");

                    GeoPoint point = new GeoPoint((int)(LAT * 1E6), (int)(LNG * 1E6));
                    OverlayItem overlayitem = new OverlayItem(point, "TEXT", null);

                    List<Overlay> mapOverlays = mapview.getOverlays();
                    Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
                    myItemizedOverlay itemizedoverlay = new myItemizedOverlay(drawable, this);

                    MapController mapController = mapview.getController();

                    //mapController.animateTo(point);
                    mapController.setZoom(8);

                    itemizedoverlay.addOverlay(overlayitem);
                    mapOverlays.add(itemizedoverlay);
                }
            }
            catch(JSONException e1)
            {
            Toast.makeText(getBaseContext(), "Error displaying contents" ,Toast.LENGTH_LONG).show();
            }       
                catch (ParseException e1) 
                {
                e1.printStackTrace();
                }   
    }
    @Override
    protected boolean isRouteDisplayed() 
    {
        return false;
    }
}

这是我的ItemizedOverlay.java代码:

    import java.util.ArrayList;

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

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


    public class myItemizedOverlay extends ItemizedOverlay 
    {

        private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
        private Context mContext;

        public myItemizedOverlay(Drawable marker, Context context) 
        {
            super(boundCenterBottom(marker));
            mContext = context;

        }

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


        @Override
        protected OverlayItem createItem(int i) 
        {
          return mapOverlays.get(i);
        }

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

        @Override
        protected boolean onTap(int index) 
        {
          OverlayItem item = mapOverlays.get(index);
          AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
          dialog.setTitle(item.getTitle());
          dialog.setMessage(item.getSnippet());
          dialog.show();
          return true;
        }

    }

我希望得到任何帮助,谢谢你。

1 个答案:

答案 0 :(得分:0)

如果您最近生成了这些密钥,则无法获得结果,因为API v1已弃用且不起作用。

您可能需要切换到Maps API v2:https://developers.google.com/maps/documentation/android/

请勿尝试使用旧代码,因为新API不会与API v1共享任何代码。