# Probleme mit Google-Maps



## florian1984 (23. Jun 2015)

Hi 

Ich versuche auf einer Karte ca 50 Marker anzeigen zulassen, was  auch gelingt. Jedoch ist es so dass wenn man auf der Karte hin und herscrollt die Karte sehr langsam läuft.  Ich habe noch nicht sooo viel Programmiererfahrung mit Java und Android und komme da nicht weiter. Ich habe schon es mit Cameralistener versucht, der Touchlistener  funktioniert leider nicht.... und den ClusterManager kann ich nicht einbinden als Libary, ausserdem kann man da glaube ich nicht mehr seine eigenen Marker benutzen...

Vielleicht kann mir ja jmd helfen....
Ich würde mich freun wenn mir jmd helfen könnte..... 




```
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.ClusterManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;




public class MapFragment extends Fragment {
    private static final String TAG = MapFragment.class.getSimpleName();

    private GoogleMap mMap;
    private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
    private HashMap<Marker, MyMarker> mMarkersHashMap;
    private LatLngBounds bounds;
    private  ArrayList<MyMarker> addedMarkers;
    List<LatLng> toBeAdded;
    private Button btn1;



    private View view;
    private FragmentActivity myContext;
    //private ClusterManager<Place> mClusterManager;


   
    private int site;
    /*
    * These are the variables */
    String[] title;
    String[] myicon;

    private boolean satellite;

    private LatLng Points;
    private String iconPath;
    private ClusterManager<MyItem> mClusterManager;


    public static MapFragment newInstance(String site, String iconpath) {

        MapFragment f = new MapFragment();
        Bundle b = new Bundle();
        b.putString("sitenumber", site);
        b.putString("iconpath", iconpath);

        f.setArguments(b);

        return f;
    }

    /*
    * Setting bounds where the icons are visible
    * */
    LatLngBounds.Builder builder = new LatLngBounds.Builder();

    ContentDatasource content = ContentDatasource.getInstance(myContext);

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }

        String sitenumber = getArguments().getString("sitenumber");
        String iconpath = getArguments().getString("iconpath");
        setIcon(iconpath);
        site = Integer.parseInt(sitenumber);


        view = (LinearLayout) inflater.inflate(R.layout.activity_maps,
                container, false);



        satellite = true;

        //view.setOnTouchListener(this);
        btn1 = (Button)view.findViewById(R.id.show_button);

        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                setsatellitemode();
            }
        });


        //Getting the latitudes and longitudes 

        ArrayList<LocationHelper_Class> locations = content.expendlocation(site);
        // Initialize the HashMap for Markers and MyMarker object
        mMarkersHashMap = new HashMap<Marker, MyMarker>();
        LocationHelper_Class Parkingarea = new de.appdream.view.Parkingarea();
        myicon = new String[] {"icon1","icon2","icon3","icon4","icon5","icon6"};



       for(int i = 0; i < locations.size()  ; i++ ) {

            int iconnumber = manageicons(locations , i, site );
            LocationHelper_Class location = locations.get(i);
           mClusterManager = new ClusterManager<MyItem>(myContext, mMap);

            if(location instanceof de.appdream.view.Parkingarea){
                location  = (de.appdream.view.Parkingarea)location;
                mMyMarkersArray.add(new MyMarker(myicon[iconnumber], location.getTitle(), location.getDisabled(), location.getNormal(),
                        location.getLatitude(), location.getLongitude(), ((de.appdream.view.Parkingarea) location).getPavement(), ((de.appdream.view.Parkingarea) location).getHouse(), ((de.appdream.view.Parkingarea) location).getGravel()));
                //Setting borders to zoom
                Points = new LatLng(location.getLatitude(), location.getLongitude());
               // MyItem offsetItem = new MyItem(location.getLatitude(), location.getLongitude());
                //Log.e(TAG, String.valueOf(offsetItem));
                //mClusterManager.addItem(offsetItem);
               // builder.include(Points);

            }else{
                mMyMarkersArray.add(new MyMarker(myicon[iconnumber],locations.get(i).getTitle(), locations.get(i).getDisabled(), locations.get(i).getNormal(),
                        locations.get(i).getLatitude(), locations.get(i).getLongitude()));
                Points= new LatLng( locations.get(i).getLatitude(), locations.get(i).getLongitude());
                //builder.include(Points);
               // MyItem offsetItem = new MyItem(locations.get(i).getLatitude(), locations.get(i).getLongitude());
                //Log.e(TAG, String.valueOf(offsetItem));
                //mClusterManager.addItem(offsetItem);
            }

        }
      //  bounds = builder.build();

        if (mMap == null)
        {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapid))
                    .getMap();
            //((MapFragment) getSupportFragmentManager()findFragmentById(R.id.mapid)).getMap();


            // Check if we were successful in obtaining the map.

            if (mMap != null)
            {
                addedMarkers = new ArrayList<MyMarker>();
                //mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
                mMap.setMyLocationEnabled(true);

                mMap.setBuildingsEnabled(true);
                mMap.setIndoorEnabled(true);
               // mMap.setTrafficEnabled(true);
                //mMap.set



               /* mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {


                    @Override
                    public void onCameraChange(CameraPosition cameraPosition) {
                        Projection projection = mMap.getProjection();
                        LatLngBounds bounds = projection.getVisibleRegion().latLngBounds;
                        Log.e(TAG, "   camera moving");

                        // add markers inside VisibleRegion:
                        //Iterator<MyMarker> addedMarkersIterator = addedMarkers.iterator();
                        //while (addedMarkersIterator.hasNext()) {
                          for(int i = 0; i < addedMarkers.size(); i++){
                            //MyMarker marker = addedMarkersIterator.next();
                            //LatLng position = marker.getPosition();
                            //MyMarker element = addedMarkers.next();
                            Double latitudecamera = addedMarkers.get(i).getlatitude();
                            Double longitudecamera = addedMarkers.get(i).getlongitude();
                            Points = new LatLng(latitudecamera,longitudecamera);

                            if (bounds.contains(Points)) {
                                //marker.remove();
                                mMyMarkersArray.add(addedMarkers.get(i));
                                addedMarkers.remove(addedMarkers.get(i));
                                //addedMarkersIterator.remove();
                            }

                        }

                        // remove markers outside of VisibleRegion:
                        //ArrayList<MyMarker> markers;
                        //private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
                        BitmapDescriptor icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA);
                        MarkerOptions options = new MarkerOptions().icon(icon);
                        Iterator <MyMarker> toBeAddedIterator = mMyMarkersArray.iterator();
                        //while (toBeAddedIterator.hasNext()) {        //mit for durchlaufen und an added übergeben
                        for(int i = 0; i < mMyMarkersArray.size() ; i++){
                            //MyMarker element = toBeAddedIterator.next();
                            Double latitudecamera = mMyMarkersArray.get(i).getlatitude();
                            Double longitudecamera = mMyMarkersArray.get(i).getlongitude();
                            Points = new LatLng(latitudecamera,longitudecamera);

                            if (!bounds.contains(Points)) {
                                icon = (BitmapDescriptorFactory.fromResource(manageMarkerIcon(mMyMarkersArray.get(i).getmIcon())));
                                options = new MarkerOptions().icon(icon);
            /*Marker marker =*/ //mMap.addMarker(options.position(Points));
                                //addedMarkers.add(mMyMarkersArray.get(i));
                                //mMyMarkersArray.remove(mMyMarkersArray.get(i));
                                //toBeAddedIterator.remove();
                            //}
                     //   }
                   // }


              //  });*/

                //mMap.setOnTouchListener(this);
                mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
                        marker.showInfoWindow();
                        return true;
                    }
                });
            }
            else
                Toast.makeText(getActivity().getApplicationContext(), "Unable to create Map", Toast.LENGTH_SHORT).show();

        }


        //setUpMap();
        return view;

    }
    private void plotMarkers(ArrayList<MyMarker> markers){
        int count = 0;
        mClusterManager = new ClusterManager<MyItem>(myContext, mMap);
       // markers = addItems();
         //toBeAdded = new ArrayList<LatLng>();
        if(markers.size() > 0) {
           for (int i = 0; i < 10; i++) {
            for (MyMarker myMarker : markers){

                double offset = i / 60d;
                mClusterManager = new ClusterManager<MyItem>(myContext, mMap);
                mMap.setOnCameraChangeListener(mClusterManager);
                mMap.setOnMarkerClickListener(mClusterManager);
                Double latidude= myMarker.getlatitude();
                Double longitude= myMarker.getlongitude();
                Points =  new LatLng(latidude + offset, longitude + offset);
                //addItems();

                MyItem offsetItem = new MyItem(myMarker.getlatitude(), myMarker.getlongitude());

                //latidude =+ offset;
                //longitude =+  offset;
                mClusterManager.addItem(offsetItem);

                MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getlatitude(), myMarker.getlongitude()));

                mMap.setOnCameraChangeListener(mClusterManager);
                mMap.setOnMarkerClickListener(mClusterManager);


                //toBeAdded.add(Points);
                //markerOption.icon(BitmapDescriptorFactory.fromResource(manageMarkerIcon(myMarker.getmIcon())));
                //Marker currentMarker = mMap.addMarker(markerOption);
                //mMarkersHashMap.put(currentMarker, myMarker)
                count++;

                mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());


                /*if(count > 20 && site == 1){
                    break;
                }*/




            }
        }  }
    }



    public void onStart() {
        super.onStart();
        plotMarkers(mMyMarkersArray);
        //CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 550, 550, 20);
        //mMap.animateCamera(cu);
    }
    @Override
    public void onResume() {
        super.onResume();
        //CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 550, 550, 20);
        //mMap.animateCamera(cu);

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.8, 12.57), 10));

        //plotMarkers(mMyMarkersArray);
        //new Title().execute();
        //CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 550, 550, 20);
        //mMap.animateCamera(cu);

    }



    public int manageicons(ArrayList<LocationHelper_Class>location , int i, int site )/*Object location*)*/{

        int number = 0;

        if(site == 0){
            if((location.get(i).getDisabled() != 0) && (location.get(i).getNormal() == 0) ){
                number = 4;
            }else if((location.get(i).getDisabled() != 0) && (location.get(i).getNormal() != 0)){
                number = 5;
            }else{
                number = 3;
            }
        }else{
            if((location.get(i).getDisabled() != 0) && (location.get(i).getNormal() == 0) ){
                number = 1;
            }else if((location.get(i).getDisabled() != 0) && (location.get(i).getNormal() != 0)){
                number = 2;
            }else{
                number = 0;
            }

        }
        return number;
    }



    private void setUpMap()
    {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null)
        {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapid))
                    .getMap();
           //((MapFragment) getSupportFragmentManager()findFragmentById(R.id.mapid)).getMap();


            // Check if we were successful in obtaining the map.

            if (mMap != null)
            {

                //mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));


                mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
                {
                    @Override
                    public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker)
                    {
                        marker.showInfoWindow();
                        return true;
                    }
                });
            }
            else
                Toast.makeText(getActivity().getApplicationContext(), "Unable to create Maps", Toast.LENGTH_SHORT).show();
        }
    }

    private int manageMarkerIcon(String markerIcon) {
        if (markerIcon.equals("icon1"))
            return R.drawable.parking;
        else if(markerIcon.equals("icon2"))
            return R.drawable.parkingdis;
        else if(markerIcon.equals("icon3"))
            return R.drawable.parkingdisandnotdis;
        else if(markerIcon.equals("icon4"))
            return R.drawable.toiletnor;
        else if(markerIcon.equals("icon5"))
            return R.drawable.disabled;
        else if(markerIcon.equals("icon6"))
            return R.drawable.disabledandnotdisabled;
        else
            return android.R.drawable.sym_def_app_icon;
    }


    @Override
    public void onAttach(Activity activity) {
        myContext = (FragmentActivity) activity;
        super.onAttach(activity);
    }

    public void setIcon(String iconPath) {
        this.iconPath = iconPath;
    }
    public String getIcon() {
        return iconPath;
    }




    public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter{
        public MarkerInfoWindowAdapter()
        {
        }

        @Override
        public View getInfoWindow(Marker marker)
        {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker){

            View v  = myContext.getLayoutInflater().inflate(R.layout.info_window_toilets, null);

            MyMarker myMarker = mMarkersHashMap.get(marker);

            ImageView markerIcon = (ImageView) v.findViewById(R.id.imageicon);

            TextView title = (TextView)v.findViewById(R.id.title_info_window);

            
            TextView not_disabled = (TextView)v.findViewById(R.id.not_disabled);
            TextView disabled = (TextView)v.findViewById(R.id.disabled);
            TextView pavement = (TextView)v.findViewById(R.id.pavement);
            TextView gravel = (TextView)v.findViewById(R.id.gravel);
            TextView house = (TextView)v.findViewById(R.id.house);


            TextView number_not_disabled = (TextView)v.findViewById(R.id.not_disabled_number);
            TextView number_disabled = (TextView)v.findViewById(R.id.number_disabled);
            TextView number_pavement = (TextView)v.findViewById(R.id.pavement_number);
            TextView number_gravel = (TextView)v.findViewById(R.id.gravel_number);
            TextView number_house = (TextView)v.findViewById(R.id.house_number);





            //myInfoWindow.setOptions({maxWidth:400});
            //Site = 0 toilets site= 1 parking-area
            if(site==1){
                if(myMarker.getNumber_disabled() != 0){
                    disabled.setVisibility(View.VISIBLE);
                    number_disabled.setVisibility(View.VISIBLE);
                    disabled.setText(": ");
                    number_disabled.setText(Integer.toString(myMarker.getNumber_disabled()));
                }
                if(myMarker.getNumber_not_disabled() != 0){
                    not_disabled.setVisibility(View.VISIBLE);
                    number_not_disabled.setVisibility(View.VISIBLE);
                    not_disabled.setText(": ");
                    number_not_disabled.setText(Integer.toString(myMarker.getNumber_not_disabled()));
                }
                if(myMarker.getPavement()!= 0){
                    pavement.setVisibility(View.VISIBLE);
                    number_pavement.setVisibility(View.VISIBLE);
                    pavement.setText(": ");
                    number_pavement.setText(Integer.toString(myMarker.getPavement()));
                }
                if(myMarker.getGravel()!= 0){
                    gravel.setVisibility(View.VISIBLE);
                    number_gravel.setVisibility(View.VISIBLE);
                    gravel.setText(": ");
                    number_gravel.setText(Integer.toString(myMarker.getGravel()));
                }
                if(myMarker.getHouse()!= 0){
                    house.setVisibility(View.VISIBLE);
                    number_house.setVisibility(View.VISIBLE);
                    house.setText(": ");
                    number_house.setText(Integer.toString(myMarker.getHouse()));
                }
            }else if(site == 0){
                if(myMarker.getNumber_disabled() != 0){
                    disabled.setVisibility(View.VISIBLE);
                    number_disabled.setVisibility(View.VISIBLE);
                    disabled.setText(": ");
                    number_disabled.setText(Integer.toString(myMarker.getNumber_disabled()));
                }
                if(myMarker.getNumber_not_disabled() != 0){
                    not_disabled.setVisibility(View.VISIBLE);
                    number_not_disabled.setVisibility(View.VISIBLE);
                    not_disabled.setText(": ");
                    number_not_disabled.setText(Integer.toString(myMarker.getNumber_not_disabled()));
                }
            }

            markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));
            title.setText(myMarker.gettitle());



            markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));

            ViewGroup.LayoutParams layoutParams = markerIcon.getLayoutParams();
            layoutParams.width = 120;
            layoutParams.height = 80;
            markerIcon.setLayoutParams(layoutParams);

            return v;
        }
    }

        public boolean get_Sattelitemode(){
            return this.satellite;
        }
        public void setSatellite(boolean satellite){
            this.satellite = satellite;
        }
        public void setsatellitemode(){
            if(get_Sattelitemode()== true){
                mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                setSatellite(false);
            }else{
                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                setSatellite(true);
            }
        }



    /*public boolean onTouch(View view, MotionEvent event){
        //mGestureDetector.onTouchEvent(event);


        int action = event.getAction();

        Log.e(TAG, " Your in onTouch Method ");

        switch (action) {

            case MotionEvent.ACTION_DOWN:
                // initialX = event.getX();
                //initialY = event.getY();

                Log.e(TAG, "Action was DOWN");
                break;

            case MotionEvent.ACTION_MOVE:
                Log.e(TAG, "Action was MOVE");
                setMovecamera(true);
                break;



            case MotionEvent.ACTION_UP:
                float finalX = event.getX();
                float finalY = event.getY();
                setMovecamera(false);

                Log.e(TAG, "Action was UP");
                break;

            case MotionEvent.ACTION_CANCEL:
                Log.e(TAG,"Action was CANCEL");
                break;

            case MotionEvent.ACTION_OUTSIDE:
                Log.e(TAG, "Movement occurred outside bounds of current screen element");
                break;
        }

        return true;
    }*/



}
```


----------

