未找到当前位置时,未显示标记上的InfoWindow

时间:2016-04-02 02:19:47

标签: android google-maps-markers google-maps-android-api-2

我正在开发一个实现MapsView的应用。在MapsView中,有一些标记,我点击其中一个标记,它会显示InfoWindow。如果找到我当前的位置,它工作正常。但问题是当我找不到当前位置时(GPS禁用),当我点击它时,标记没有显示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();
        }
    }
}

任何答案都将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:0)

我通过在marker.showInfoWindow();

中添加此代码onMarkerClick来解决问题