重新输入地图活动后,地图停止工作

时间:2019-03-21 16:42:48

标签: java android

当前,我的地图应用程序从Web服务器获取坐标,并将这些值放在Google地图“ LatLng”中。第一次输入可正常使用,但是,当我按下后退按钮并再次重新输入活动而应用程序崩溃时,它似乎来自服务器方法的getLocation不会再次开始。我已经在下面提供了代码。我仍然是学习Java和android studio的初学者。

主要活动

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://teamdronex.com/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();
    UserClient userClient = retrofit.create(UserClient.class);



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

        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.btn_secret).setOnClickListener(this);

    }
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.btn_secret:
                getSecret();
                break;
            case R.id.btn_Map:
                getMap();
                break;
        }

    }

    private static String token;

    private void login() {
        Login login = new Login("johndoe@example.com", "aaaaaa");
        Call<User> call = userClient.login(login);

        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(MainActivity.this, response.body().getToken(), Toast.LENGTH_SHORT).show();
                    token = response.body().getToken();
                } else {
                    Toast.makeText(MainActivity.this, "login not correct", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getSecret() {
        Call<ResponseBody> call = userClient.getSecret(token);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "token is not correct", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();

            }
        });
    }



    private void getMap() {
        Intent intent = new Intent(MainActivity.this, GMap.class);
        startActivity(intent);

    }
}

    #Gmap Activity
    public class GMap extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private  String lat;
    private  String lng;


    public GMap(){
        this.getLocation();
    }


    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://teamdronex.com/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();
    UserClient userClient = retrofit.create(UserClient.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gmap);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


    }
     private void getLocation() {
        Call<Location> call = userClient.getLocation();

        call.enqueue(new Callback<Location>() {
            @Override
            public void onResponse(Call<Location> call, Response<Location> response) {
                if (response.isSuccessful()) {


                    lat = response.body().getGps_latitude();
                    lng = response.body().getGps_longitude();
                    Log.e("Lat","INSIDE getLocation");
                    Log.e("Lat","lat"+ lat);

                } else {
                    Toast.makeText(GMap.this, "login not correct", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<Location> call, Throwable t) {
                Toast.makeText(GMap.this, "error", Toast.LENGTH_SHORT).show();

            }

        });

    }


    @Override
    public void onMapReady(GoogleMap googleMap) {


        Log.e("result","smt " + this.lat);
        double Lat = Double.parseDouble(this.lat);
        double Lng = Double.parseDouble(this.lng);


        mMap = googleMap;



        LatLng sydney = new LatLng(Lat, Lng);

        mMap.addMarker(new MarkerOptions().position(sydney).title("Home"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(17);
        mMap.animateCamera(zoom);
    }
}

0 个答案:

没有答案