简体   繁体   中英

Google Maps API on Android not accepting Lat and Long values?

so I wanted to access a remote mySQL database and access the lat and long co-ordinates stored there. I wrote a config.php and fetch.php, to get the data using JSON, and in this script, the latVal and longVal values are not loading (They are blank) inside the onMapReady() method, whereas they contain accurate values in the getJSON() method. Why is it happening?

code:

package com.rageking.xyber.dashboardtest;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.telecom.Connection;
import android.view.View;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.*;
import java.util.HashMap;

import com.google.android.gms.common.api.Response;
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.MarkerOptions;

import org.json.JSONException;
import org.json.JSONObject;

public class ChildTrackerPage extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    ConnectionHTTP myCon = new ConnectionHTTP();

    String latVal, longVal;
    double latNum, longNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child_tracker_page);
        getJSON("http://webgax.com/schoolapp/fetch.php");
        // 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);

    }

    public void goToMenu(View view) {
        Intent i1 = new Intent(this, UserHome.class);
        startActivity(i1);
    }

    private void getJSON(final String urlWebService) {

        class GetJSON extends AsyncTask<String, Void, String> {

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

            @Override
            public void onPostExecute(String s) {
                super.onPostExecute(s);
                //Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
                try {

                    JSONObject jsonObject = new JSONObject(s);
                    longVal = jsonObject.getString("lat");
                    latVal = jsonObject.getString("lng");
                   //Toast.makeText(getApplicationContext(),longVal, Toast.LENGTH_SHORT).show();
                   //Toast.makeText(getApplicationContext(),latVal,Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public String doInBackground(String... strings) {
                HashMap<String,String> hashMap=new HashMap<>();
                return myCon.postRequest(hashMap, "http://webgax.com/schoolapp/fetch.php");

            }
        }

        GetJSON getJSON = new GetJSON();
        getJSON.execute();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        //latNum =Double.parseDouble(latVal);
        //longNum = Double.parseDouble(longVal);

        //latNum = Double.valueOf(latVal);
        //longNum = Double.valueOf(longVal);

        Toast.makeText(getApplicationContext(),latVal,Toast.LENGTH_LONG).show();
        //Toast.makeText(getApplicationContext(),longVal,Toast.LENGTH_LONG).show();

        LatLng addr = new LatLng(22, 88);

        float zoomLevel = 16; //This goes up to 21

        mMap.addMarker(new MarkerOptions().position(addr).title("WebGax"));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(addr, zoomLevel));

        }
    }

As mentioned in the comments, you should just try to update the map after the map is ready to be updated... With your initial code you had the AsyncTask being called before which depending on the time of response of the server could result on the map being ready or not when you had the lat and long values.

Check my suggestion below, I'm simply removing the call for your async task from the onCreate call and moving it to be after the map is ready to receive the data.

public class ChildTrackerPage extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    private ConnectionHTTP myCon = new ConnectionHTTP();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child_tracker_page);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    public void goToMenu(View view) {
        Intent i1 = new Intent(this, UserHome.class);
        startActivity(i1);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        GetJSON getJSON = new GetJSON();
        getJSON.execute();
    }

    private class GetJSON extends AsyncTask<String, Void, String> {

        @Override
        public String doInBackground(String... strings) {
            HashMap<String,String> hashMap=new HashMap<>();
            return myCon.postRequest(hashMap, "http://webgax.com/schoolapp/fetch.php");
        }

        @Override
        public void onPostExecute(String s) {
            JSONObject jsonObject = new JSONObject(s);
            //I'm not sure why you do that instead of just using optDouble on your JSON but I don't know the contect of your JSON
            String longVal = jsonObject.getString("lat");
            String latVal = jsonObject.getString("lng");
            double latNum = Double.parseDouble(latVal);
            double longNum = Double.parseDouble(longVal);

            LatLng addr = new LatLng(latNum, longNum);
            float zoomLevel = 16; //This goes up to 21

            if(mMap!=null){
                mMap.addMarker(new MarkerOptions().position(addr).title("WebGax"));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(addr, zoomLevel));
            }
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM