从Web服务获取Google Map Marker和数据

时间:2017-03-30 08:09:00

标签: android google-maps

我被困在这一点上,我必须从网络服务中调用Google Map Marker,我该怎么做my web service。我尝试的是下面的代码不起作用。我想要做的是使用 Picasso 库从Web服务加载图标。
我没有太多关于谷歌地图的知识。如果有人帮助我,那对我来说会很好。

public class Frnt_mapActivity extends Activity {

    public String Shop_title;
    public String Shop_address;
    public String Shop_icons;
    ProgressDialog mDialog;
    JSONObject jsonobject3;
    JSONArray jsonarray3;
    // Google Map
    private static GoogleMap googlemap;


    public static ArrayList<SearchBeams> searchdata_list;
    public static ArrayList<NormalSearchBeams> normlSearchList;


    ArrayList<All_products_lat_long> venueList;


    private int i;
    private Intent intent;
    HashMap<Marker, Integer> hashMap=new HashMap<Marker, Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frnt_map_activity);

        googlemap=((MapFragment)getFragmentManager().findFragmentById(R.id.places_map)).getMap();
        googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googlemap.setMyLocationEnabled(true);
        googlemap.getUiSettings().setZoomControlsEnabled(true);
        googlemap.getUiSettings().setMyLocationButtonEnabled(true);
        googlemap.getUiSettings().setCompassEnabled(true);
        googlemap.getUiSettings().setRotateGesturesEnabled(true);
        googlemap.getUiSettings().setZoomGesturesEnabled(true);
        googlemap.setMyLocationEnabled(true);
        intent=new Intent(Frnt_mapActivity.this,MainActivity.class);
        googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

            @Override
            public void onInfoWindowClick(Marker arg0) {
                i=hashMap.get(arg0);
                intent.putExtra("PRODUCT_ID", venueList.get(i).getId());
                startActivity(intent);
                //overridePendingTransition( R.anim.slide_right, R.anim.slide_left );
            }
        });


        new Frnt_mIcons_Activity().execute();


    }
    private void addMarkers() {




        for(int k=0; k<venueList.size(); k += 1){

            //nirmal
            try{
                Log.e("pj",venueList.get(k).getMap_icon());

                //new AddMarkerAsyncTask(venueList.get(k),k).execute();

                double ln = Double.parseDouble(venueList.get(k).getLongitude());
                double la = Double.parseDouble(venueList.get(k).getLatitude());
                String title=venueList.get(k).getPost_title()==null?"":venueList.get(k).getPost_title();
                URL url = new URL(venueList.get(k).getMap_icon());
                Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                BitmapDescriptor bd=BitmapDescriptorFactory.fromBitmap(image);

                Marker marker=googlemap.addMarker(new MarkerOptions()
                        .position(new LatLng(la, ln))
                        .title(title)
                        .icon(bd));
                hashMap.put(marker, k);

            }catch (Exception e){
                Log.e("addMarkers",k+"");
                e.printStackTrace();
            }


        }
    }

    /******************FOR MAP******************/
    class Frnt_mIcons_Activity extends AsyncTask<String, String, String> {

        public ArrayList<All_products_lat_long> maplist;


        @Override
        protected void onPreExecute() {
            mDialog = new ProgressDialog(Frnt_mapActivity.this);
            mDialog.setMessage("Loading...");
            mDialog.setCancelable(false);
            mDialog.show();
            super.onPreExecute();
        }




        // Slow Implementation
        private String inputStreamToString(InputStream is) {
            String s = "";
            String line = "";
            // Wrap a BufferedReader around the InputStream
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            // Read response until the end
            try {
                while ((line = rd.readLine()) != null) {
                    s += line;
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Return full string
            return s;
        }


        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub  
            maplist = new ArrayList<All_products_lat_long>();

            HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

            DefaultHttpClient client = new DefaultHttpClient();

            SchemeRegistry registry = new SchemeRegistry();
            SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
            socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
            registry.register(new Scheme("http", socketFactory, 443));
            SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
            DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

    // Set verifier
                HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

            HttpPost httpPost = new HttpPost("http://towncitycards.com/webservice_action.php?action=all_products");

            try{

                HttpResponse response = httpClient.execute(httpPost);
                String data = inputStreamToString(response.getEntity().getContent());
                jsonobject3 = new JSONObject(data);
                jsonarray3 = new JSONArray(jsonobject3.getString("all_products"));
                venueList =new ArrayList<All_products_lat_long>();
                for(int j=0; j<jsonarray3.length();j++){
                    JSONObject itemobj = jsonarray3.getJSONObject(j);
                    //SHOW SEARCH RESULT
                    All_products_lat_long searchItems = new All_products_lat_long();

                    searchItems.setId(itemobj.getString("ID"));
                    searchItems.setPost_title(itemobj.getString("post_title"));
                    searchItems.setMap_icon(itemobj.getString("map_icon"));
                    searchItems.setLongitude(itemobj.getString("longitude"));
                    searchItems.setLatitude(itemobj.getString("latitude"));
                    //          System.out.println("######Ashish is object >>"+itemobj);
                    venueList.add(searchItems);
                    //      System.out.println("SEARCHLISTSS@@@@@@@"+venueList.get(j).getLongitude());
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }


        @Override
        public void onPostExecute(String result) {
            super.onPostExecute(result);

            //System.out.println("SEARCHLISTSS@@@@@@@"+venueList);
            //System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAA"+venueList);
            try{
                if(venueList.size()>0)
                {
                    addMarkers();
                    //Toast.makeText(Frnt_mapActivity.this,"Search for the venue11.",Toast.LENGTH_SHORT).show();

                }
                else
                    Toast.makeText(Frnt_mapActivity.this,"Search for the venue.",Toast.LENGTH_SHORT).show();
                //LatLng coordinate = new LatLng(22.75852160, 75.8911550);
                LatLng coordinate = new LatLng(50.85514, 0.58382);
                CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 18);
                googlemap.animateCamera(yourLocation);
                for (int i = 0; i < jsonarray3.length(); i++) {

                    jsonobject3 = jsonarray3.getJSONObject(i);

                    Shop_title = jsonobject3.optString("post_title");
                    Shop_address = jsonobject3.optString("map_icon");
                    Shop_icons = jsonobject3.optString("map_icon");
                    //}
                }


            }catch(Exception e){
                e.printStackTrace();
            }

            if(mDialog!=null && mDialog.isShowing()){
                mDialog.dismiss();
            }
        }
    }

}

记录:

03-30 14:47:19.467 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/5percent.png
03-30 14:47:20.798 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/save£2.png
03-30 14:47:20.798 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png
03-30 14:47:20.799 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png
03-30 14:47:20.800 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png
03-30 14:47:20.800 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png
03-30 14:47:20.801 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/freedrink.png
03-30 14:47:20.801 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png
03-30 14:47:20.802 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png
03-30 14:47:20.802 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png
03-30 14:47:20.803 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png
03-30 14:47:20.803 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png
03-30 14:47:20.804 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/freedrink.png
03-30 14:47:20.804 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/freedessert.png
03-30 14:47:20.837 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/save£2.png
03-30 14:47:20.837 2895-2895/com.towncitycards E/pj: http://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/10percent.png

获取所有网址但不显示图标 这是一个模拟器图像: enter image description here

1 个答案:

答案 0 :(得分:0)

试试这段代码:

导入:

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

` 创建一个类:

 public class PicassoMarker implements Target {
        Marker mMarker;

        PicassoMarker(Marker marker) {
            mMarker = marker;
        }

        @Override
        public int hashCode() {
            return mMarker.hashCode();
        }

        @Override
        public boolean equals(Object o) {
            if (o instanceof PicassoMarker) {
                Marker marker = ((PicassoMarker) o).mMarker;
                return mMarker.equals(marker);
            } else {
                return false;
            }
        }

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    }

现在将addMarkers()方法修改为:

 private void addMarkers() {
    for (int k = 0; k < venueList.size(); k += 1) {
        try {
            Log.e("pj", venueList.get(k).getMap_icon());

            //new AddMarkerAsyncTask(venueList.get(k),k).execute();

            double ln = Double.parseDouble(venueList.get(k).getLongitude());
            double la = Double.parseDouble(venueList.get(k).getLatitude());
            String title = venueList.get(k).getPost_title() == null ? "" : venueList.get(k).getPost_title();

            Marker marker = googlemap.addMarker(new MarkerOptions()
                    .position(new LatLng(la, ln))
                    .title(title));

            PicassoMarker pmarker = new PicassoMarker(marker);
            Picasso.with(MainActivity.this).setLoggingEnabled(true).fit().load(venueList.get(k).getMap_icon()).into(pmarker);
            hashMap.put(marker, k);

        } catch (Exception e) {
            Log.e("addMarkers", k + "");
            e.printStackTrace();
        }
    }
}

PicassoMarker课程受到Answer

的启发

我在您的addMarkers()方法

中添加了一些修改