2016-04-02 16 views
-1

私はMapsViewを実装するアプリケーションを開発しています。 MapsViewには、クリックしたマーカーの1つが表示され、InfoWindowが表示されます。現在の場所が見つかった場合は正常に動作します。しかし問題は、現在の場所が見つからない(GPSが無効になっている)ときで、マーカーがInfoWindowをクリックしたときに表示されないときです。現在の場所が見つからない場合、InfoWindowはマーカーに表示されません。

はここで、任意の答えは非常に理解されるであろう

//Getting current location 
private void getCurrentLocation() { 
    mMap.clear(); 

    //Creating a location object 
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 
     location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
    } else { 
     Toast.makeText(MapsActivity.this, "Anda harus menyetujuinya agar dapat menggunakan semua fitur yang ada", Toast.LENGTH_LONG).show(); 
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
     } 
    } 

    if (location != null) { 
     //Getting longitude and latitude 
     fromLongitude = location.getLongitude(); 
     fromLatitude = location.getLatitude(); 

     //Creating a LatLng Object to store Coordinates 
     origin = new LatLng(fromLatitude, fromLongitude); 

     mo=new MarkerOptions().position(origin).icon(BitmapDescriptorFactory.fromResource(R.mipmap.here)); 
     my_marker=mMap.addMarker(mo); 
     my_marker.setTitle("I'm Here"); 
     my_marker.setSnippet("Starting point"); 
     my_marker.setDraggable(true); 
    } 
} 

class GetInfo extends AsyncTask<String, Void, Boolean> { 
    ProgressDialog dialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialog = new ProgressDialog(MapsActivity.this); 
     dialog.setMessage("Mohon tunggu"); 
     dialog.setTitle("Mendapatkan data..."); 
     dialog.show(); 
     dialog.setCancelable(true); 
    } 

    @Override 
    protected Boolean doInBackground(String... urls) { 
     try { 
      HttpGet httppost = new HttpGet(urls[0]); 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse response = httpclient.execute(httppost); 
      int status = response.getStatusLine().getStatusCode(); 
      if (status == 200) { 

       HttpEntity entity = response.getEntity(); 
       String data = EntityUtils.toString(entity); 
       JSONObject jsono = new JSONObject(data); 

       JSONArray konten = jsono.getJSONArray("konten"); 
       mMarkersHashMap = new HashMap<Marker, MyMarker>(); 

       for (int i = 0; i < konten.length(); i++) { 
        HashMap<String,String> newMap=new HashMap<String,String>(); 
        JSONObject object = konten.getJSONObject(i); 
        newMap.put("nama", object.getString("nama")); 
        newMap.put("deskripsi",object.getString("deskripsi")); 
        newMap.put("foto",object.getString("foto")); 
        newMap.put("marker", object.getString("marker")); 
        newMap.put("lat", object.getString("lat")); 
        newMap.put("lng", object.getString("lng")); 
        array.add(newMap); 
       } 
       return true; 
      } 
     } catch (ParseException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return false; 
    } 

    protected void onPostExecute(final Boolean result) { 
     dialog.cancel(); 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       if (!result) { 
        //Toast.makeText(getApplicationContext(), "Tidak dapat mengambil data dari server, silahkan cek koneksi internet anda", Toast.LENGTH_LONG).show(); 
        showInfo(); 
       } 
       else { 
        for (int i = 0; i < array.size(); i++) { 
         HashMap<String, String> newMap = new HashMap<String, String>(); 
         newMap = array.get(i); 
         mMyMarkersArray.add(new MyMarker(newMap.get("nama"), newMap.get("deskripsi"), newMap.get("foto"), newMap.get("marker"), Double.parseDouble(newMap.get("lat")), Double.parseDouble(newMap.get("lng")))); 
        } 
        plotMarkers(mMyMarkersArray); 
       } 
       } 
     }); 

    } 
} 

@Override 
public void onConnected(Bundle bundle) { 
    getCurrentLocation(); 
} 

public void plotMarkers(ArrayList<MyMarker> markers) { 
    if(markers.size() > 0) { 
     for (MyMarker myMarker : markers) 
     { 
      dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()); 
      markerOption = new MarkerOptions().position(dest); 
      location_marker = mMap.addMarker(markerOption); 
      Target target = new PicassoMarker(location_marker); 
      targets.add(target); 

      ImageView image = new ImageView(this); 
      image.setImageResource(R.mipmap.marker); 
      int width = image.getDrawable().getIntrinsicWidth(); 
      int height = image.getDrawable().getIntrinsicHeight(); 

      Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(width, height).onlyScaleDown().into(target); 
      mMarkersHashMap.put(location_marker, myMarker); 

      i = getIntent(); 
      if(i.getBooleanExtra("maps", true)) { 
       location_marker.setTitle(i.getStringExtra("nama")); 
       mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dest, 16)); 
      } 
      else { 
       mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter()); 
      } 
     } 
    } 
} 

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter 
{ 
    @Override 
    public View getInfoWindow(Marker marker) { 
     return null; 
    } 

    @Override 
    public View getInfoContents(Marker marker) { 
     View v = getLayoutInflater().inflate(R.layout.info_windowlayout, null); 

     MyMarker myMarker = mMarkersHashMap.get(marker); 

     TextView markerLabel = (TextView) v.findViewById(R.id.marker_label); 
     markerLabel.setText(myMarker.getmLabel()); 

     ImageView foto = (ImageView) v.findViewById(R.id.foto); 
     Picasso.with(getApplicationContext()).load(myMarker.getmImage()).placeholder(R.layout.progress).error(R.mipmap.error).into(foto); 

     TextView anotherLabel = (TextView) v.findViewById(R.id.another_label); 
     anotherLabel.setText("Baca selengkapnya..."); 

     if (myMarker.getmImage() != null) { 
      Picasso.with(getApplicationContext()) 
        .load(myMarker.getmImage()) 
        .placeholder(R.layout.progress) 
        .into(foto, new MarkerCallback(marker)); 
     } 

     return v; 
    } 
} 

public class MarkerCallback implements Callback { 
    Marker marker=null; 

    MarkerCallback(Marker marker) { 
     this.marker=marker; 
    } 

    @Override 
    public void onError() { 
     Log.e(getClass().getSimpleName(), "Tidak dapat mengambil gambar!"); 
    } 

    @Override 
    public void onSuccess() { 
     if (marker != null && marker.isInfoWindowShown()) { 
      marker.hideInfoWindow(); 
      marker.showInfoWindow(); 
     } 
    } 
} 

私のコードです。

ありがとうございました。

+0

でこのコードmarker.showInfoWindow();を追加することで問題を解決? –

+0

編集した投稿を確認してください。私は 'GetInfo'クラスの' onPostExecute'で 'plotMarkers'を呼び出します。 –

+0

コール mMap.setInfoWindowAdapter(新しいMarkerInfoWindowAdapter()); on の –

答えて

0

私はあなたがplotMarkers関数を呼び出すんonMarkerClick

関連する問題