quinta-feira, 9 de abril de 2015

MapaFragment

public class MapaFragment extends Fragment {

    public static final String REQUEST_onListItemClick_Mapa = "onListItemClick_MapaFragment";
    public static final String REQUEST_onLoadPoints = "REQUEST_onLoadPoints";
    ArrayList listPointsObject = new ArrayList();
    MapView gmapView;
    GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mapa_layout, container, false);
        gmapView = (MapView) view.findViewById(R.id.gmapView);
        if(gmapView!=null) {
            gmapView.onCreate(savedInstanceState);
            gmapView.onResume();
            try {
                MapsInitializer.initialize(getActivity().getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
            Response response = new Response(REQUEST_onLoadPoints);
            ((IResponse)this.getActivity()).onResponse(response);
        }
        return view;
    }


    private class MapItem{
        public LatLng coordenadas;
        public Object Item;
        public MapItem(LatLng coordenadas, Object Item){
            this.coordenadas=coordenadas;
            this.Item=Item;
        }
    }

    protected Object findObjectByPosition(double latitude, double longitude){
        for(int i=0;i            MapItem item = listPointsObject.get(i);
            if(item.coordenadas.latitude==latitude
                    && item.coordenadas.longitude==longitude){
                return  item.Item;
            }
        }
        return null;
    }

    public void LocalizacaoAtual(Location location, Boolean Zoom)
    {
        if(gmapView!=null &&  gmapView.getMap()!=null) {
            googleMap = gmapView.getMap();
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title(Constants.STRING_LOCALIZACAO_ATUAL);
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                    .alpha(0.7f);
            googleMap.addMarker(marker);

            if (Zoom) {
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(latitude, longitude)).zoom(AppConfig.DEFAULT_GOOGLE__MAPS_ZOOM).build();
                googleMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));
            }
        }
    }

    public void AtualizarDados(EmpresaConveniada[] values)
    {
        if(gmapView!=null &&  gmapView.getMap()!=null) {
            googleMap = gmapView.getMap();
            googleMap.getUiSettings().setZoomControlsEnabled(true);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.getUiSettings().setCompassEnabled(true);
            googleMap.getUiSettings().setRotateGesturesEnabled(true);
            googleMap.getUiSettings().setZoomGesturesEnabled(true);
            googleMap.clear();
            double cameraLatitude = 0;
            double cameraLongitude = 0;

           // getLocation

            for (int i = 0; i < values.length; i++) {

                double longitude = Double.parseDouble(values[i].Longitude);
                double latitude = Double.parseDouble(values[i].Latitude);

                if (cameraLatitude == 0) {
                    cameraLatitude = latitude;
                    cameraLongitude = longitude;
                }

                MarkerOptions marker = new MarkerOptions().position(
                        new LatLng(latitude, longitude)).title(values[i].Nome);
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
                        .alpha(0.7f);

                listPointsObject.add(new MapItem(new LatLng(latitude, longitude), values[i]));

                googleMap.addMarker(marker);
                googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(Marker arg0) {
                        Object objectDetail = findObjectByPosition(arg0.getPosition().latitude, arg0.getPosition().longitude);
                        Activity activity = getActivity();
                        Response response = new Response(REQUEST_onListItemClick_Mapa);
                        response.Object = objectDetail;
                        ((IResponse) activity).onResponse(response);
                        return true;
                    }

                });

            }
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(cameraLatitude, cameraLongitude)).zoom(AppConfig.DEFAULT_GOOGLE__MAPS_ZOOM).build();
            googleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(gmapView!=null)
            gmapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if(gmapView!=null)
            gmapView.onPause();
    }

}
Postar um comentário