如何从onResume调用onMapReady()

时间:2018-11-05 08:36:52

标签: android methods maps

我想从onResume调用方法onMapReady。问题是它具有

  

(GoogleMap地图)

我不能这样称呼

  

onMapReady();

这是方法

@Override
public void onMapReady(GoogleMap map) {
    GMap = map;
    GMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(49.8117806, 15.6970293), 6.0f));
    if (!getloc)
        getLocation();
}

更新 这是整个班级

public class Map extends AppCompatActivity implements LocationListener, OnMapReadyCallback, NavigationView.OnNavigationItemSelectedListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar.setTitle("");
    toolbar.setSubtitle("");

    //gpsStatus = (TextView) findViewById(R.id.gpsStatus);
    dis = (TextView) findViewById(R.id.distancePreview);

    newActL = (RelativeLayout) findViewById(R.id.newAct);
    startActL = (RelativeLayout) findViewById(R.id.Act);
    timer = (TextView) findViewById(R.id.timePreview);

    newActB = (ImageButton) findViewById(R.id.newActB);
    stopActB = (ImageButton) findViewById(R.id.stopActB);
    gearMap = (ImageButton) findViewById(R.id.gearmap);

    List<String> spinnerArray = new ArrayList<String>();
    spinnerArray.add("Arrival");
    spinnerArray.add("Departure");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, spinnerArray);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sItems = (Spinner) findViewById(R.id.ardep);
    sItems.setAdapter(adapter);

    List<String> spinnerArray2 = new ArrayList<String>();
    spinnerArray2.add("Running");
    spinnerArray2.add("Walking");
    spinnerArray2.add("Cycling");
    spinnerArray2.add("Roller skating");
    spinnerArray2.add("Skateboarding");
    spinnerArray2.add("Kickbiking");
    spinnerArray2.add("Teleporting");

    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, spinnerArray2);

    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    actCombo = (Spinner) findViewById(R.id.actCombo);
    actCombo.setAdapter(adapter2);
    Calendar rightNow = Calendar.getInstance();
    int currentHour = rightNow.get(Calendar.HOUR_OF_DAY);
    if (currentHour < 12) {
        actCombo.setSelection(GetInfo.arract - 1);
        sItems.setSelection(0);
    } else {
        actCombo.setSelection(GetInfo.depact - 1);
        sItems.setSelection(1);
    }

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            3000,
            1, this);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview);
    mapFragment.getMapAsync(this);
    line = new PolylineOptions().width(3).color(Color.BLUE);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    headerView = navigationView.getHeaderView(0);
    fullnameside = (TextView) headerView.findViewById(R.id.fullnameside);
    emailside = (TextView) headerView.findViewById(R.id.emailside);
    fullnameside.setText("" + GetInfo.fullname);
    emailside.setText("" + GetInfo.email);

    navigationView.setNavigationItemSelectedListener(this);
    navigationView.getMenu().getItem(1).setChecked(true);

}

@Override
public void onResume(){
    super.onResume();
}

private void getLocation() {
    //Toast.makeText(getBaseContext(), "e paides", Toast.LENGTH_SHORT).show();
    String locationProvider = LocationManager.NETWORK_PROVIDER;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = locationManager.getLastKnownLocation(locationProvider);
    try {
        line.add(new LatLng(location.getLatitude(), location.getLongitude()));
        GMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title(""));
        GMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16.0f));
        steps++;
        getloc = true;
    } catch (NullPointerException e) {
        //Toast.makeText(this.getBaseContext(), "gyhg" + e.toString(), Toast.LENGTH_LONG).show();
    }
}

public void StopTrip(View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Stop and upload your trip?")
            .setCancelable(true)
            .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    float finalDistance = (float) (distance / 1000.0);
                    if (finalDistance < 2) {
                        Intent myIntent = new Intent(Map.this, Profile.class);
                        startActivity(myIntent);
                    }
                    TimeBuff += MillisecondTime;
                    handler.removeCallbacks(runnable);
                    // newActL.setVisibility(View.GONE);
                    // startActL.setVisibility(View.VISIBLE);
                    enabledActivity = false;
                    //database post
                    try {
                        Date currentTime = Calendar.getInstance().getTime();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        String formattedDate = df.format(currentTime);

                        Date currentTime2 = Calendar.getInstance().getTime();
                        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd%20HH:mm:ss");
                        dateActEnd = df2.format(currentTime2);

                        String act = actCombo.getSelectedItem().toString();
                        String act_id = "7";
                        switch (act) {
                            case "Running":
                                act_id = "1";
                                break;
                            case "Walking":
                                act_id = "2";
                                break;
                            case "Cycling":
                                act_id = "3";
                                break;
                            case "Roller skating":
                                act_id = "4";
                                break;
                            case "Skateboarding":
                                act_id = "5";
                                break;
                            case "Kickbiking":
                                act_id = "6";
                                break;
                            case "Teleporting":
                                act_id = "7";
                                break;
                        }

                        String direcor;
                        if (sItems.getSelectedItem().toString().equals("Arrival"))
                            direcor = "arrival";
                        else
                            direcor = "departure";

                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                        StrictMode.setThreadPolicy(policy);
                        // String.format("%.1f", finalDistance)
                        URL obj = new URL("https://gekon.technologypark.cz/api/v1/record/create?user=" + LoginInfo.UserID
                                + "&date=" + formattedDate + "&distance=" + String.format("%.1f", finalDistance) + "&direction=" + direcor
                                + "&activity=" + act_id + "&polyline=" + PolyUtil.encode(line.getPoints()) + "&start=" + dateActStart
                                + "&end=" + dateActEnd + "&source=mobileapp");

                        HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty("ApiSecret", LoginInfo.ApiSecret);
                        conn.connect();
                        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
                        StringBuilder sb = new StringBuilder();
                        String output;
                        while ((output = br.readLine()) != null)
                            sb.append(output);

                        JSONObject jsonObj = new JSONObject(sb.toString());
                        JSONObject curRecord = new JSONObject(jsonObj.getString("data"));

                        Trips.datet.add(currentTime);
                        Trips.datestr.add(formattedDate);
                        Trips.act.add(act_id);
                        Trips.tripType.add(sItems.getSelectedItem().toString());
                        Trips.dist.add(String.format("%.1f", finalDistance));
                        Trips.trip_ids.add(curRecord.getString("trip_id"));
                        Trips.calc(++Trips.points);

                        TripsCalendarInfo.datet.add(currentTime);
                        TripsCalendarInfo.act.add(act_id);
                        TripsCalendarInfo.act_str.add(act);
                        TripsCalendarInfo.tripType.add(sItems.getSelectedItem().toString());
                        TripsCalendarInfo.dist.add(String.format("%.1f", finalDistance));
                        TripsCalendarInfo.datestr.add(formattedDate);
                        TripsCalendarInfo.trip_ids.add(curRecord.getString("trip_id"));
                        TripsCalendarInfo.trip_source.add("mobileapp");
                        TripsCalendarInfo.polyline.add(PolyUtil.encode(line.getPoints()));
                        TripsCalendarInfo.CanItrip();

                        float km_up = Float.parseFloat(TripsInfo.km.get(TripsInfo.userRank - 1)) + finalDistance;
                        int trip_up = Integer.parseInt(TripsInfo.trips.get(TripsInfo.userRank - 1)) + 1;
                        TripsInfo.trips.set(TripsInfo.userRank - 1, "" + trip_up);
                        TripsInfo.km.set(TripsInfo.userRank - 1, String.format("%.1f", km_up));
                        TripsInfo.rankSort();

                        getloc = false;

                        Intent myIntent = new Intent(Map.this, Profile.class);
                        startActivity(myIntent);

                    } catch (Exception e) {
                        Toast.makeText(getBaseContext(), "Error upload, please check your options at gear button", Toast.LENGTH_LONG).show();
                    }
                }
            });
    float finalDistance = (float) (distance / 1000.0);
    if (finalDistance < 2) {
        builder.setMessage("Your trip is below 2km and it will not be counted.\nAre you sure you want to stop?");
    }
    AlertDialog alert = builder.create();
    alert.show();

}


public void StartAct(View v) {
    timer.setVisibility(View.VISIBLE);
    dis.setVisibility(View.VISIBLE);
    newActB.setVisibility(View.GONE);
    stopActB.setVisibility(View.VISIBLE);
    //gearMap.setVisibility(View.GONE);
    navigationView.setVisibility(View.GONE);
    //headerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    toolbar.setNavigationIcon(null);          // to hide Navigation icon
    //toolbar.setDisplayHomeAsUpEnabled(false);
    handler = new Handler();

    Date currentTime = Calendar.getInstance().getTime();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd%20HH:mm:ss");
    dateActStart = df.format(currentTime);
    StartTime = SystemClock.uptimeMillis();
    handler.postDelayed(runnable, 0);

    startService(new Intent(this, LocationService.class));
    //enabledActivity = true;
}

public void ChangeAct(View v) {
    if (newActL.getVisibility() == View.GONE) {
        newActL.setVisibility(View.VISIBLE);
        startActL.setVisibility(View.GONE);
    } else {
        newActL.setVisibility(View.GONE);
        startActL.setVisibility(View.VISIBLE);
    }
}

public Runnable runnable = new Runnable() {

    public void run() {

        MillisecondTime = SystemClock.uptimeMillis() - StartTime;

        UpdateTime = TimeBuff + MillisecondTime;

        Seconds = (int) (UpdateTime / 1000);

        Minutes = Seconds / 60;

        Hours = Minutes / 60;

        Seconds = Seconds % 60;

        Minutes = Minutes % 60;

        MilliSeconds = (int) (UpdateTime % 100);

        timer.setText("Time: " + String.format("%02d", Hours) + ":"
                + String.format("%02d", Minutes) + ":"
                + String.format("%02d", Seconds));

        handler.postDelayed(this, 950);
    }

};

@Override
public void onMapReady(GoogleMap map) {
    GMap = map;
    GMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(49.8117806, 15.6970293), 6.0f));
    if (!getloc)
        getLocation();
}

2 个答案:

答案 0 :(得分:1)

这是您要找的吗?

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private Marker marker;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    button = (Button) findViewById(R.id.testButton);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MapsActivity.this, TestActivity.class);
            startActivityForResult(i, 100);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 100) {
        Log.d("MyMap", "onActivityResult " + data.getStringExtra("result"));
    }
}

@Override
protected void onPause() {
    super.onPause();

    Log.d("MyMap", "onPause");
}

@Override
protected void onResume() {
    super.onResume();

    Log.d("MyMap", "onResume");
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {

    if (mMap == null) {

        Log.d("MyMap", "setUpMapIfNeeded");
        ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMapAsync(this);
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    Log.d("MyMap", "onMapReady");
    mMap = googleMap;
    setUpMap();
}

private void setUpMap() {

    mMap.setMyLocationEnabled(true);
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    mMap.getUiSettings().setMapToolbarEnabled(false);


    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

            Log.d("MyMap", "MapClick");

            //remove previously placed Marker
            if (marker != null) {
                marker.remove();
            }

            //place marker where user just clicked
            marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

            Log.d("MyMap", "MapClick After Add Marker");

        }
    });

}
}

可以在here

找到原始讨论

答案 1 :(得分:1)

我已经把

  

getloc = false

因此每次都会调用getlocation()方法!

@Override
public void onMapReady(GoogleMap map) {
    GMap = map;
    GMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(49.8117806, 15.6970293), 6.0f));
    getloc=false; //THE NEW ADD
    if (!getloc)
    getLocation();
}