2015-06-10 95 views
1

我正在爲所有標記創建自定義infowindow,因爲我想在標記中顯示用戶圖標。我想在地圖上的標記上打開時顯示所有標記,但我無法做到這一點。想要一次顯示所有標記的所有infowindow

這是我的代碼。

public class FriendMapActivity extends FragmentActivity implements 
     InfoWindowAdapter { 
    GoogleMap googleMap; 
    Context context; 
    Marker mMarker; 
    public static int snip; 
    public static String user_id; 
    public static ArrayList<String> useridList, fullnameList, imageList, 
      latiList, LongiLIst; 
    Button btnBack; 
    Bitmap bmImg; 
    public ArrayList<Marker> marker; 
    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle arg0) { 
     // TODO Auto-generated method stub 
     super.onCreate(arg0); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_friend_map); 
     if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
        .permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
     } 
     btnBack = (Button) findViewById(R.id.btnBack); 
     SharedPreferences prefers = this 
       .getSharedPreferences("LOGIN_DETAIL", 0); 
     user_id = prefers.getString("USER_ID", ""); 

     SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.Nearmap); 

     // Getting a reference to the map 
     GetUserDetailByUserId(); 
     googleMap = supportMapFragment.getMap(); 
     googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     googleMap.setMyLocationEnabled(true); 


     googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { 

      @Override 
      public View getInfoWindow(Marker arg0) { 
       // TODO Auto-generated method stub 
       View v = getLayoutInflater().inflate(R.layout.activity_marker, 
         null); 
       LatLng latLng = arg0.getPosition(); 
       snip = Integer.parseInt(arg0.getId().substring(1)); 
       // Getting reference to the TextView to set latitude 
       ImageView imageicon = (ImageView) v 
         .findViewById(R.id.imagemarker); 
       // Getting reference to the TextView to set longitude 
       for (int i = 0; i < latiList.size(); i++) { 
        try { 
         Bitmap bmp = BitmapFactory.decodeStream(new URL(
           imageList.get(snip)).openConnection() 
           .getInputStream()); 
         imageicon.setImageBitmap(bmp); 
         Picasso.with(FriendMapActivity.this) 
           .load(imageList.get(snip)) 
           .transform(new CircleTransform()) 
           .into(imageicon); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
       return v; 
      } 

      @Override 
      public View getInfoContents(Marker arg0) { 
       // TODO Auto-generated method stub 
       if (FriendMapActivity.this.mMarker != null 
          && FriendMapActivity.this.mMarker.isInfoWindowShown()) { 
        //FriendMapActivity.this.mMarker.hideInfoWindow(); 

        FriendMapActivity.this.mMarker.showInfoWindow(); 
         for(int i=0;i<marker.size();i++) 
         { 
          FriendMapActivity.this.mMarker.showInfoWindow(); 
         } 
        } 
        return null; 

      } 
     }); 

     btnBack.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       finish(); 
      } 
     }); 

     // GetUserDetailByUserId(); 
    } 

    @Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 
    } 

    @Override 
    protected void onStop() { 
     // TODO Auto-generated method stub 
     super.onStop(); 
    } 
public void GetUserDetailByUserId() { 
     new AsyncTask<Void, Void, String>() { 
      ProgressDialog mProgressDialog; 

      @SuppressWarnings("deprecation") 
      @Override 
      protected String doInBackground(Void... params) { 
       // TODO Auto-generated method stub 

       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(
         "url of server is here"); 
       marker = new ArrayList<Marker>(); 
       try { 

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
          2); 
        nameValuePairs.add(new BasicNameValuePair("action", 
          "GetUserDetailByUserId")); 

        nameValuePairs.add(new BasicNameValuePair("userid", String 
          .valueOf(5))); 

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
        // Execute HTTP Post Request 
        HttpResponse response = httpclient.execute(httppost); 
        BufferedReader in = new BufferedReader(
          new InputStreamReader(response.getEntity() 
            .getContent())); 
        StringBuffer sb = new StringBuffer(""); 
        String line = ""; 
        while ((line = in.readLine()) != null) { 
         sb.append(line); 
        } 
        in.close(); 
        Log.e("get user want to Data", "" + sb.toString()); 
        return sb.toString(); 
       } catch (Exception e) { 
        Log.e("get user problem", "" + e); 
        return ""; 
       } 
      } 

      @Override 
      protected void onPostExecute(String result) { 
       // TODO Auto-generated method stub 
       super.onPostExecute(result); 
       useridList = new ArrayList<String>(); 
       fullnameList = new ArrayList<String>(); 
       imageList = new ArrayList<String>(); 
       latiList = new ArrayList<String>(); 
       LongiLIst = new ArrayList<String>(); 

       try { 
        mProgressDialog.dismiss(); 
        JSONObject object = new JSONObject(result.toString()); 
        if (object.getString("msg").equalsIgnoreCase("Success")) { 
         JSONArray array = object.getJSONArray("data"); 
         for (int j = 0; j < array.length(); j++) { 
          JSONObject subObj = array.getJSONObject(j); 
          useridList.add(subObj.getString("userid")); 
          fullnameList.add(subObj.getString("fullname")); 
          imageList.add(subObj.getString("image_url")); 
          latiList.add(subObj.getString("latitude")); 
          LongiLIst.add(subObj.getString("longitude")); 
          Log.d("val of userid list:", useridList.toString()); 
         } 
         for (int i = 0; i < latiList.size(); i++) { 
          takeToLocation(new LatLng(
            Double.parseDouble(latiList.get(i)), 
            Double.parseDouble(LongiLIst.get(i)))); 
          MarkerOptions markerOptions = new MarkerOptions() 
            .position(
              new LatLng(Double 
                .parseDouble(latiList 
                  .get(i)), Double 
                .parseDouble(LongiLIst 
                  .get(i)))) 
            .icon(BitmapDescriptorFactory 
              .fromResource(R.drawable.blue_dot)); 
          // // Bitmap circleBitmap = 
          // Bitmap.createBitmap(200,200, 
          // Bitmap.Config.ARGB_8888); 
          // // Bitmap bmp = 
          // Bitmap.createBitmap(circleBitmap); 
          // // Canvas canvas1 = new Canvas(bmp); 
          // // 
          // // // paint defines the text color, 
          // // // stroke width, size 
          // // Paint color = new Paint(); 
          // // color.setTextSize(12); 
          // // color.setColor(Color.BLACK); 
          // // Bitmap yourBitmap; 
          // 
          // 
          // URL url = new URL(imageList.get(i)); 
          // HttpURLConnection conn = (HttpURLConnection) 
          // url.openConnection(); 
          // conn.setDoInput(true); 
          // conn.connect(); 
          // InputStream is = conn.getInputStream(); 

          // bmImg = BitmapFactory.decodeStream(is); 

          // Bitmap resized = Bitmap.createScaledBitmap(bmImg, 
          // 100, 100, true); 
          // //modify canvas 
          // // canvas1.drawBitmap(
          // // getCircularBitmapImage(bmImg), 0,0, color); 
          // // canvas1.drawText(fullnameList.get(i), 30, 40, 
          // color); 
          // 
          // //add marker to Map 
//       googleMap.addMarker(new 
//       MarkerOptions().position(new 
//       LatLng(Double.parseDouble(latiList.get(i)), 
//       Double.parseDouble(LongiLIst.get(i)))) 
//       .icon(BitmapDescriptorFactory.fromBitmap(getCircularBitmapImage(bmImg))) 
//       // Specifies the anchor to be at a particular 
//      
//       .anchor(0.5f, 1)); 
          marker.add(mMarker); 

          mMarker = googleMap.addMarker(markerOptions); 
          mMarker.setTitle("Click Me:"); 
          for(i=0;i<marker.size();i++) 
          { 
           mMarker.showInfoWindow(); 
          } 
          //mMarker.isInfoWindowShown(); 
//       showAllMarker(i); 

         } 

        } else { 
         System.out.println("not a valid user"); 
        } 

       } catch (Exception e) { 
        Log.e("json error in gettripexperience", "" + e); 

       } 

      } 

      @Override 
      protected void onPreExecute() { 
       // TODO Auto-generated method stub 
       super.onPreExecute(); 
       mProgressDialog = new ProgressDialog(FriendMapActivity.this); 
       mProgressDialog.setTitle(""); 
       mProgressDialog.setCanceledOnTouchOutside(false); 
       mProgressDialog.setMessage("Please Wait..."); 
       mProgressDialog.show(); 

      } 
     }.execute(); 
    } 

    @Override 
    public View getInfoContents(Marker arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public View getInfoWindow(Marker arg0) { 
     // TODO Auto-generated method stub 
     View v = getLayoutInflater().inflate(R.layout.activity_marker, null); 
     LatLng latLng = arg0.getPosition(); 
     // Getting reference to the TextView to set latitude 
     ImageView tvLat = (ImageView) v.findViewById(R.id.imagemarker); 
     // Getting reference to the TextView to set longitude 

     return v; 
    } 

    public void takeToLocation(LatLng toBeLocationLatLang) { 
     if (toBeLocationLatLang != null) { 
      CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
        toBeLocationLatLang, 16); 
      googleMap.animateCamera(update); 
     } else { 
      Toast.makeText(this, "Position Unavailable", Toast.LENGTH_SHORT) 
        .show(); 
     } 
    } 

    public static Bitmap getCircularBitmapImage(Bitmap source) { 
     int size = Math.min(source.getWidth(), source.getHeight()); 
     int x = (source.getWidth() - size)/2; 
     int y = (source.getHeight() - size)/2; 
     Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); 
     if (squaredBitmap != source) { 
      source.recycle(); 
     } 
     Bitmap bitmap = Bitmap 
       .createBitmap(size, size, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     Paint paint = new Paint(); 
     BitmapShader shader = new BitmapShader(squaredBitmap, 
       BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); 
     paint.setShader(shader); 
     paint.setAntiAlias(true); 
     float r = size/6f; 
     canvas.drawCircle(r, r, r, paint); 
     squaredBitmap.recycle(); 
     return bitmap; 
    } 

    public class CircleTransform implements Transformation { 
     @Override 
     public Bitmap transform(Bitmap source) { 
      int size = Math.min(source.getWidth(), source.getHeight()); 

      int x = (source.getWidth() - size)/2; 
      int y = (source.getHeight() - size)/2; 

      Bitmap squaredBitmap = Bitmap 
        .createBitmap(source, x, y, size, size); 
      if (squaredBitmap != source) { 
       source.recycle(); 
      } 

      Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); 

      Canvas canvas = new Canvas(bitmap); 
      Paint paint = new Paint(); 
      BitmapShader shader = new BitmapShader(squaredBitmap, 
        BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); 
      paint.setShader(shader); 
      paint.setAntiAlias(true); 

      float r = size/2f; 
      canvas.drawCircle(r, r, r, paint); 

      squaredBitmap.recycle(); 
      return bitmap; 
     } 

     // protected Bitmap transform(BitmapPool pool, Bitmap source, 
     // int outWidth, int outHeight) { 
     // return getCircularBitmapImage(source); 
     // } 

     public String getId() { 
      return "Glide_Circle_Transformation"; 
     } 

     @Override 
     public String key() { 
      // TODO Auto-generated method stub 
      return "circle"; 
     } 

    } 
// function generateInfoWindow(despacedName, despacedTitle, eventTitle, url, date, time){ 
//  if(!curInfoWindow){ 
//   curInfoWindow = new google.maps.InfoWindow({maxWidth:150}); 
//  } 
//  curInfoWindow.setContent("<a class=\"eventLink\" href=\""+url+"\">"+eventTitle+"</a><br><br>"+markerObjects[despacedName].title+"<br>"+date+" at "+time); 
//  curInfoWindow.open(map, markerObjects[despacedName]); 
//  alert(despacedName+" is "+markerObjects[despacedName]); 
// } 

    public void showAllMarker(int pos) { 
     MarkerOptions markerOptions = new MarkerOptions().position(
       new LatLng(Double.parseDouble(latiList.get(pos)), Double 
         .parseDouble(LongiLIst.get(pos)))).icon(
       BitmapDescriptorFactory.fromResource(R.drawable.blue_dot)); 
     mMarker = googleMap.addMarker(markerOptions); 
     if (mMarker.isInfoWindowShown()) { 
      Log.d("marker:", mMarker.getId()); 
     } else { 
      mMarker.showInfoWindow(); 
     } 
    } 
+0

這是不是很好的方式來顯示所有標記信息窗口,而不是顯示自定義用戶圖標作爲標記檢查此:https://github.com/googlemaps/android-maps-utils –

+0

什麼是確切的你面臨的問題? –

+0

我想在地圖中一次顯示所有infowindow,但是當我單擊一個標記時,另一個標記上的Infowindow隱藏了我如何解決此問題? –

回答

相關問題