Google地图显示徽标,但不显示在Android模拟器中

时间:2018-10-17 21:11:27

标签: java android google-maps

我的应用程序应该显示带有热图的Google地图,但是我什至无法显示该地图。它仅在左下角显示徽标。似乎没有错误。我还检查了gradle,所有必要的依赖项都在那里。我不知道这是由于实际错误还是缺少内存/处理缓慢。

这是MainActivity

package com.example.alexlevine.oceanapp;

package com.example.alexlevine.oceanapp;

import android.app.Dialog;
import android.content.Intent;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.maps.android.heatmaps.HeatmapTileProvider;
import com.google.maps.android.heatmaps.WeightedLatLng;

import android.view.View;

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

import static com.example.alexlevine.oceanapp.R.id.seekBar;
import static com.example.alexlevine.oceanapp.fetchSOCATData.co2array;

public class MainActivity extends AppCompatActivity 
implements OnMapReadyCallback {

public static SeekBar seekbar;

public static GoogleMap mGoogleMap;
private HeatmapTileProvider mProvider;

public static TextView minyear;
public static TextView maxyear;
public static TextView currentyeartext;
public static int displayyear;
public static boolean usingSOCAT;
public static String typeKey;
public static Spinner dropdown;

private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
android.support.v7.app.ActionBarDrawerToggle mDrawerToggle;

Button b2;
Button b4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (googleServicesAvailable()) {
        Toast.makeText(this, "Play services available", Toast.LENGTH_LONG).show();;
        initMap();

        b2 = (Button) findViewById(R.id.button2);
        b4 = (Button) findViewById(R.id.button4);

        b2.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (v.getId() == R.id.button2) {
                            Intent i = new Intent(MainActivity.this, CarbonCalcActivity.class);
                            startActivity(i);
                            finish();
                        }
                    }
                });

        b4.setOnClickListener(
                new View.OnClickListener() {
                   @Override
                    public void onClick(View v) {
                       if(v.getId() == R.id.button4) {
                           Intent i = new Intent(MainActivity.this, CalcData.class);
                           startActivity(i);
                           finish();
                       }
                   }
                }
        );

        //get the spinner from the xml.
        dropdown = (Spinner)findViewById(R.id.spinner);
        //create a list of items for the spinner.
        String[] items = new String[]{"Ocean CO2 Densities", "Land CO2 Emissions (all sources)", "Electricity CO2 Emissions", "Aviation CO2 Emissions", "Ocean Shipping CO2 Emissions"};
        //create an adapter to describe how the items are displayed, adapters are used in several places in android.
        //There are multiple variations of this, but this is the basic variant.
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
        //set the spinners adapter to the previously created one.
        dropdown.setAdapter(adapter);


        currentyeartext = (TextView) findViewById(R.id.currentyear);

        minyear = (TextView) findViewById(R.id.minyear);
        maxyear = (TextView) findViewById(R.id.maxyear);

        typeKey = "00totals";
        dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                switch (position) {
                    case 0: {
                        usingSOCAT = true;
                        displayyear = 1991;
                        seekbar.setMax(24);
                        minyear.setText("1991");
                        maxyear.setText("2015");
                        break;
                    }
                    case 1: {
                        usingSOCAT = false;
                        displayyear = 1997;
                        minyear.setText("1997");
                        maxyear.setText("2010");
                        seekbar.setMax(14);
                        typeKey = "00totals";
                        break;
                    }
                    case 2: {
                        usingSOCAT = false;
                        displayyear = 1997;
                        minyear.setText("1997");
                        maxyear.setText("2010");
                        seekbar.setMax(14);
                        typeKey = "01elec";

                        // Whatever you want to happen when the thrid item gets selected
                        break;
                    }
                    case 3: {
                        usingSOCAT = false;
                        displayyear = 1997;
                        minyear.setText("1997");
                        maxyear.setText("2010");
                        seekbar.setMax(14);
                        typeKey = "11aviation";;
                        // Whatever you want to happen when the thrid item gets selected
                        break;
                    }
                    case 4: {
                        usingSOCAT = false;
                        displayyear = 1997;
                        minyear.setText("1997");
                        maxyear.setText("2010");
                        seekbar.setMax(14);
                        typeKey = "12shipping";
                        // Whatever you want to happen when the thrid item gets selected
                        break;
                    }
                }
            }
            public void onNothingSelected(AdapterView<?> parent)
            {

            }
        });

        seekbar = (SeekBar) findViewById(seekBar);
        seekbar.setOnSeekBarChangeListener(
                new SeekBar.OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                        displayyear = progress + 1991;
                        if(usingSOCAT)
                        {
                            displayyear = progress + 1991;

                            new fetchSOCATData().execute();

                        }
                        else
                        {
                            displayyear = progress + 1997;
                            new fetchFFDASData().execute();
                            //List<WeightedLatLng> calledData = fetchFFDASData.ffdasArray;

                            //createHeatMap(fetchFFDASData.ffdasArray);
                            minyear.setText((CharSequence) fetchFFDASData.ffdasArray);
                        }
                        currentyeartext.setText("Showing: " + displayyear);
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {

                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {

                    }
                });

        if(usingSOCAT) {

        }
        //new fetchSOCATData().execute();
        /*minyear = (TextView) findViewById(R.id.minyear);
        maxyear = (TextView) findViewById(R.id.maxyear);
        minyear.setText("1957");
        maxyear.setText("2015");*/

        //fetchSOCATData process = new fetchSOCATData();
        //process.execute();

    } else {
        //No maps layout
    }
}

//@Override
public void onNothingSelected(AdapterView<?> parent) {

}

public void initMap() {
   SupportMapFragment mapFragment = (SupportMapFragment) this.getSupportFragmentManager()
           .findFragmentById(R.id.mapFragment);

   if(mapFragment != null) {
       mapFragment.getMapAsync((OnMapReadyCallback)this);
   }
}

public void createHeatMap(List<WeightedLatLng> dataset)
{
   List<WeightedLatLng> locations = dataset;
   /*mProvider = new HeatmapTileProvider.Builder().weightedData(locations).build();
   mProvider.setRadius( HeatmapTileProvider.DEFAULT_RADIUS );
   mGoogleMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
    mProvider = new HeatmapTileProvider();
    mProvider.setWeightedData(dataset);
    mProvider = mProvider.build();*/

    //for (LatLng coordinate : coordinates) {
        //WeightedLatLng weightedCoordinate = new WeightedLatLng(coordinate);
        //com.google.maps.android.geometry.Point point = weightedCoordinate.getPoint();

        // Filter points at infinity
       /* if (Double.isInfinite(point.x) || Double.isInfinite(point.y)) {
            Log.w(TAG, "Attempted to add undefined point " + coordinate);
            continue;
        }
        weightedCoordinates.add(weightedCoordinate);*/
    //}

    mProvider = new HeatmapTileProvider.Builder()
            .weightedData(dataset)
            .opacity(0.5)
            .build();
    mGoogleMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
    Toast.makeText(this, "working", Toast.LENGTH_SHORT).show();
}

public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
    //goToLocation(-65, -12);
}

public void goToLocation(double lat, double lng) {
    LatLng ll = new LatLng(lat, lng);
    CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
    mGoogleMap.moveCamera(update);
}

public boolean googleServicesAvailable() {
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int isAvailable = api.isGooglePlayServicesAvailable(this);
    if (isAvailable == ConnectionResult.SUCCESS)
        return true;
    else if (api.isUserResolvableError(isAvailable)) {
        Dialog dialog = api.getErrorDialog(this, isAvailable, 0);
        dialog.show();
    } else
        Toast.makeText(this, "Can't connect to play services", Toast.LENGTH_LONG).show();
    return false;
}

}

这是我的代码,用于获取热图数据:

package com.example.alexlevine.oceanapp;

import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.google.android.gms.maps.model.LatLng;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.maps.android.heatmaps.WeightedLatLng;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.alexlevine.oceanapp.MainActivity.displayyear;
import static com.example.alexlevine.oceanapp.MainActivity.minyear;



@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
public class fetchSOCATData extends AsyncTask<Object, Object, ArrayList<WeightedLatLng>> {
//String data = "http://ferret.pmel.noaa.gov/socat/erddap/tabledap/socat_v4_fulldata.geoJson?year%2Cmonth%2Cday%2Chour%2Cminute%2Csecond%2Clongitude%2Clatitude%2Csal%2CTemperature_equi%2CTemperature_atm%2CpCO2_atm_wet_actual%2Cdelta_fCO2%2Crelative_humidity%2Cspecific_humidity%2Cwind_speed_true%2Cwind_speed_rel%2Cwind_dir_true%2Cwind_dir_rel%2CfCO2_recommended%2Cregion_id%2Ctime&organization=%22%22";
//public static SOCATCO2DataPoint.FeaturesBean[] co2array;
public static List<WeightedLatLng> co2array;

@Override
protected ArrayList<WeightedLatLng> doInBackground(Object... params) {

    co2array = new ArrayList<WeightedLatLng>();

    int year = displayyear;

    URL url = null;
    try {
        //url = new URL("http://ferret.pmel.noaa.gov/socat/erddap/tabledap/socat_v4_fulldata.geoJson?year%2Cmonth%2Cday%2Chour%2Cminute%2Csecond%2Clongitude%2Clatitude%2Csal%2CTemperature_equi%2CTemperature_atm%2CpCO2_atm_wet_actual%2Cdelta_fCO2%2Crelative_humidity%2Cspecific_humidity%2Cwind_speed_true%2Cwind_speed_rel%2Cwind_dir_true%2Cwind_dir_rel%2CfCO2_recommended%2Cregion_id%2Ctime&organization=%22%22&year%3E=2015&month%3C=1&day%3C=1&hour%3C=1&minute%3C=1&second%3C=10");
        //url = new URL("http://ferret.pmel.noaa.gov/socat/erddap/tabledap/socat_v4_fulldata.json?year%2Clongitude%2Clatitude%2CfCO2_recommended%2Ctime&organization=%22%22&year%3E=" + year + "&year%3C=" + year);
        //url = new URL("http://ferret.pmel.noaa.gov/socat/erddap/tabledap/socat_v4_fulldata.json?year%2Clongitude%2Clatitude%2CfCO2_recommended%2Ctime&organization=%22%22&year%3E=2015&year%3C=2015&month%3E=1&month%3C=1&day%3E=1&day%3C=1&hour%3E=1&hour%3C=1");
        //url = new URL("http://ferret.pmel.noaa.gov/socat/erddap/tabledap/socat_v4_fulldata.geoJson?year%2Cmonth%2Cday%2Chour%2Cminute%2Csecond%2Clongitude%2Clatitude%2CfCO2_recommended%2Ctime&organization=%22%22&year%3E=" + year + "&year%3C=" + year);

        url = new URL("http://ferret.pmel.noaa.gov/socat/erddap/tabledap/socat_v4_fulldata.json?longitude%2Clatitude%2CfCO2_recommended&organization=%22%22&year=" + displayyear + "&day=1&hour=1&minute%3C=5&second%3C=30&longitude!=NaN&longitude!=NaN&latitude!=NaN&latitude!=NaN");
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }

    OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(url).build();
    Response response = null;
    try {
        response = client.newCall(request).execute();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    String result = null;
    try {
        result = response.body().string();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    //System.out.print("result" + result);

    Gson gson = new Gson();

    Type collectionType = new TypeToken<Collection<OfficialSOCATPoint.TableBean>>() {}.getType();
    OfficialSOCATPoint enums = gson.fromJson(result, OfficialSOCATPoint.class);
    //Collection<OfficialSOCATPoint.TableBean> enums = gson.fromJson(result, collectionType);
    /*SOCATCO2DataPoint.FeaturesBean[] protoExtract = enums.toArray(new SOCATCO2DataPoint.FeaturesBean[enums.size()]);*/
    OfficialSOCATPoint protoExtract = gson.fromJson(result, OfficialSOCATPoint.class);
    //OfficialSOCATPoint.TableBean[] protoExtract = enums.toArray(new OfficialSOCATPoint.TableBean[enums.size()]);

    List<List<String>> dataArray = (List<List<String>>) protoExtract.getTable().getRows();

    //for (int i = 0; i < dataArray.size(); i++)
    for (int i = 0; i < 2; i++)
    {
        if(dataArray.get(i).get(2) != "null" && dataArray.get(i).get(1) != "null" && dataArray.get(i).get(0) != "null")
        {
            //co2array.add(new WeightedLatLng(new LatLng(Double.parseDouble(dataArray.get(i).get(0)), Double.parseDouble(dataArray.get(i).get(1))), Double.parseDouble(dataArray.get(i).get(2))));
        }

    }

    return (ArrayList<WeightedLatLng>) co2array;
    }

//@Override
protected void onPostExecute(ArrayList<WeightedLatLng> fb) {
    super.onPostExecute(fb);
}
}

这是logcat:

10-17 16:45:27.884 1583-1593/? W/WallpaperManagerService: Cannot extract colors because wallpaper could not be read.
10-17 16:45:27.886 1583-1593/? W/WallpaperManagerService: Cannot extract colors because wallpaper could not be read.
10-17 16:45:17.747 2160-2160/? W/zygote: Common causes for lock verification issues are non-optimized dex code
10-17 16:45:17.955 1583-1807/? W/WallpaperManagerService: Cannot extract colors because wallpaper could not be read.
10-17 16:45:19.896 2138-2227/? W/zygote: Common causes for lock verification issues are non-optimized dex code
10-17 16:45:28.114 2188-2312/? W/ErrorProcessor: onFatalError, processing error from engine(4)
                                             com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream

at com.google.android.apps.gsa.staticplugins.recognizer.j.a.a(SourceFile:28)

at com.google.android.apps.gsa.staticplugins.recognizer.j.b.run(SourceFile:15)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)

at java.util.concurrent.FutureTask.run(FutureTask.java:266)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)

at java.util.concurrent.FutureTask.run(FutureTask.java:266)

at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(Unknown Source:4)

at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)

at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)

at java.lang.Thread.run(Thread.java:764)

at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6)

Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.

at com.google.android.apps.gsa.speech.audio.Tee.f(SourceFile:103)

at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)

at java.io.InputStream.read(InputStream.java:101)

at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:18)

at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457) 

at java.util.concurrent.FutureTask.run(FutureTask.java:266) 

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457) 

at java.util.concurrent.FutureTask.run(FutureTask.java:266) 

at com.google.android.apps.gsa.shared.util.concurrent.a.ag.run(Unknown Source:4) 

at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4) 

at com.google.android.apps.gsa.shared.util.concurrent.a.bo.run(SourceFile:4) 

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 

at java.lang.Thread.run(Thread.java:764) 

at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:6) 
10-17 16:45:38.815 2510-2510/? I/Finsky: [2] com.google.android.finsky.setup.VpaService.a(32): Should not show workaround PAI step because experiment disabled
10-17 16:45:48.416 1767-2047/? I/GCoreUlr: WorldUpdater:init: Ensuring that reporting is stopped because of reasons: (no Google accounts)
10-17 16:45:49.205 2069-2069/? I/MediaRouter: Unselecting the current route because it is no longer selectable: null
10-17 16:46:16.401 1767-2047/? W/NetworkScheduler: Immediate task was not started com.google.android.videos/.service.drm.RefreshLicenseTaskService{u=0 tag="refresh_license_forced" trigger=window{start=0s,end=1s,earliest=-1s,latest=0s} requirements=[NET_CONNECTED] attributes=[] scheduled=-1s last_run=N/A jid=N/A status=PENDING retries=0 client_lib=MANCHEGO_GCM-10400000}. Rescheduling immediate tasks can cause excessive battery drain.
10-17 16:46:16.928 1767-2047/? W/NetworkScheduler: Immediate task was not started com.google.android.gms/.tapandpay.gcmtask.TapAndPayGcmTaskService{u=0 tag="immediate" trigger=window{start=0s,end=1s,earliest=-1s,latest=0s} requirements=[NET_CONNECTED] attributes=[] scheduled=-1s last_run=N/A jid=N/A status=PENDING retries=0 client_lib=MANCHEGO_GCM-14366000}. Rescheduling immediate tasks can cause excessive battery drain.

请帮助我。谢谢!

0 个答案:

没有答案