OutOfMemoryError android google maps

时间:2016-10-20 19:42:51

标签: android google-maps android-studio google-maps-api-3

I have an application with the following information in google maps, the issue is that lately I have been being blocked. I'd appreciate any help.

public class MapaGeneral extends FragmentActivity implements GoogleMap.OnMyLocationChangeListener {

    // Google Map
    private GoogleMap googleMap;

    // Latitude & Longitude
    private Double Latitude = 0.00;
    private Double Longitude = 0.00;

    private int id_tienda = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mapa_general);
        MultiDex.install(this);


        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        ArrayList<HashMap<String, String>> location = null;
        String url = "http://www.lifes.com.co/vet_life/info_map_general.php";
        try {

            JSONArray data = new JSONArray(getHttpGet(url));

            location = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> map;

            for(int i = 0; i < data.length(); i++){
                JSONObject tienda = data.getJSONObject(i);

                map = new HashMap<String, String>();
                map.put("lat", tienda.getString("lat"));
                map.put("lng", tienda.getString("lng"));
                map.put("tienNombre", tienda.getString("tienNombre"));
                map.put("tienDescripcion", tienda.getString("tienDescripcion"));
                location.add(map);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // *** Display Google Map
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        googleMap.clear();


        // *** Focus & Zoom
        Latitude = Double.parseDouble(location.get(0).get("lat").toString());
        Longitude = Double.parseDouble(location.get(0).get("lng").toString());
        LatLng coordinate = new LatLng(Latitude, Longitude);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.setMyLocationEnabled(true);

        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 24));
        if (ActivityCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            return;
        }


        // *** Marker (Loop)
        for (int i = 0; i < location.size(); i++) {
            Latitude = Double.parseDouble(location.get(i).get("lat").toString());
            Longitude = Double.parseDouble(location.get(i).get("lng").toString());
            String name = location.get(i).get("tienNombre").toString();
            String descip = location.get(i).get("tienDescripcion").toString();
            MarkerOptions marker = new MarkerOptions().position(new LatLng(Latitude, Longitude)).title(name).snippet(descip);
            googleMap.setOnMyLocationChangeListener(this);
            googleMap.addMarker(marker);

        }

        googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Intent iratienda = new Intent(getApplicationContext(),Informacion_Cliente.class);
                LatLng lat_long = marker.getPosition();
                iratienda.putExtra("lat", lat_long.latitude);
                iratienda.putExtra("long", lat_long.longitude);
                startActivity(iratienda);
            }
        });
    }

    @Override
    public void onMyLocationChange(Location loc) {
        LatLng myposicion = new LatLng(loc.getLatitude(),loc.getLongitude());
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myposicion, 14));
        googleMap.setOnMyLocationChangeListener(null);
    }

    public static String getHttpGet(String url) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) { // Download OK
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
            } else {
                Log.e("Log", "Failed to download result..");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();
    }

}

and this is the error:

This is the error in my proyect: 

E/dalvikvm-heap: Out of memory on a 333264-byte allocation.
E/dalvikvm-heap: Out of memory on a 1134016-byte allocation.
E/AndroidRuntime: FATAL EXCEPTION: GLThread 11503
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.OutOfMemoryError
	at android.graphics.Bitmap.nativeCreate(Native Method)
	at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
	at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
	at android.graphics.Bitmap.createBitmap(Bitmap.java:670)
	at maps.ad.G.a(Unknown Source)
	at maps.D.c.a(Unknown Source)
	at maps.D.d.c(Unknown Source)
	at maps.ad.S.g(Unknown Source)
	at maps.ad.T.b(Unknown Source)
	at maps.D.c.a(Unknown Source)
	at maps.V.k.a(Unknown Source)
	at maps.V.u.d(Unknown Source)
	at maps.V.P.onSingleTapConfirmed(Unknown Source)
	at maps.z.e$b.onSingleTapConfirmed(Unknown Source)
	at maps.z.c$a.handleMessage(Unknown Source)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loop(Looper.java:137)
	at android.app.ActivityThread.main(ActivityThread.java:5493)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:525)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
	at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案