Hack Forums
[Java] How to pass latitude and Longitude from sqlite database to Google Maps Activity - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] How to pass latitude and Longitude from sqlite database to Google Maps Activity (/showthread.php?tid=5611997)



How to pass latitude and Longitude from sqlite database to Google Maps Activity - VIPhiggz10 - Apr 30, 2017

I've set up a database which through a textview displays the latitude and longitude of a specific building. There is a "go" button underneath which loads up my mapsActivity. However, I can't seem to get the lat and long to be placed on the map.

Maps Activity

Code
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    GoogleMap mGoogleMap;
    SupportMapFragment mapFrag;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    Marker mCurrLocationMarker, map1, map2, map3, map4, map5, map6, map7;
    ZoomControls zoom;
   // public static String TAG1;
   // public static String TAG2;
   public static String TAG_LAT = "lat";
    public static String TAG_LNG = "lng";

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

        ToggleButton b1 = (ToggleButton) findViewById(R.id.button3);

        getSupportActionBar().setTitle("Queen's Building Search");

        Intent intent = getIntent();








        mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFrag.getMapAsync(this);

        zoom = (ZoomControls) findViewById(R.id.zcZoom);
        zoom.setOnZoomOutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mGoogleMap.animateCamera(CameraUpdateFactory.zoomOut());

            }
        });
        zoom.setOnZoomInClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mGoogleMap.animateCamera(CameraUpdateFactory.zoomIn());

            }
        });



      Bundle extras = getIntent().getExtras();
        if(extras != null){
            double value = extras.getDouble("Latitude");
            double value1 = extras.getDouble("Longitude");
           //  TAG_LAT = extras.getString("Latitude");
           // TAG_LNG = extras.getString("Longitude");
           goToLocation(value, value1);

          
        }

Go to location Method being called

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



    }

building Activity

Code
btnGoLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Double lat = selectedBuilding.getLatitude();
                Double lng = selectedBuilding.getLongitude();


                Bundle bundle = new Bundle();
               // Intent intent = new Intent(BuildingActivity.this, MapsActivity.class);
                //Bundle extras = getIntent().getExtras();

                Intent intent = new Intent(BuildingActivity.this, MapsActivity.class);
                bundle.putDouble(MapsActivity.TAG_LNG,lat);
                bundle.putDouble(MapsActivity.TAG_LNG, lng);


              //  Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
              //  intent.putExtra("Latitude", +lat);
              //  intent.putExtra("Longitude", ""+lng);






               // intent.putExtra("latitude", lat);
               // intent.putExtra("longitude", lng);

               intent.putExtras(bundle);
                startActivity(intent);



                // Double latt=extras.getDouble(lat);
                //Double loNg=extras.getDouble(lng)
            }
        });

As you can see my code is pretty messy trying to get this to work. If anyone could teamview with me it'd be a lot easier to show what i'm trying to do. I feel like i'm close.


* - SIM-429621 - Apr 30, 2017

Imagine you have Table called GeoLocations, in that table you have following columns: City, CityLongitude, CityLatitude, in order to get back values from columns CityLatitude and CityLongitude for let's say city New York, you would do something like this:

("SELECT CityLatitude FROM GeoLocations WHERE City=\"New York\""); and assign this to a variable of type double, or whatever is the kind of value you stored in the DB.

And you do the same for the CityLongitude, if you don't want to use "\" to escape double quotes, you can surround city name with single quotes:

("SELECT CityLatitude FROM GeoLocations WHERE City='New York'");

If you want to learn this and not just put shit up in order to make your app work, read this, it's short and concise and you will learn how to do basic stuff with JDBC:

https://docs.oracle.com/javase/tutorial/jdbc/

EDIT:

For more Java tutorials and courses you can look up google stuff or PM me and I will give you access to some paid courses that go into more details about Java and JDBC.


* - VIPhiggz10 - Apr 30, 2017

(Apr 30, 2017 12:56 PM)Passenger Wrote:  Imagine you have Table called GeoLocations, in that table you have following columns: City, CityLongitude, CityLatitude, in order to get back values from columns CityLatitude and CityLongitude for let's say city New York, you would do something like this:

("SELECT CityLatitude FROM GeoLocations WHERE City=\"New York\""); and assign this to a variable of type double, or whatever is the kind of value you stored in the DB.

And you do the same for the CityLongitude, if you don't want to use "\" to escape double quotes, you can surround city name with single quotes:

("SELECT CityLatitude FROM GeoLocations WHERE City='New York'");

If you want to learn this and not just put shit up in order to make your app work, read this, it's short and concise and you will learn how to do basic stuff with JDBC:

https://docs.oracle.com/javase/tutorial/jdbc/

EDIT:

For more Java tutorials and courses you can look up google stuff or PM me and I will give you access to some paid courses that go into more details about Java and JDBC.

Hi thanks for the reply. I hope that over the summer i'll be able to focus more time in getting deeper into Java. This project however needs to be complete by tuesday and this is the last piece of the puzzle before the app is ready to be demonstrated. This is in android studio. Do you think you could team view me and help fix the problem? it'd be greatly appreciated and would take a lot of stress off.


* - Khamul - May 1, 2017

(Apr 30, 2017 07:54 PM)VIPhiggz10 Wrote:  Hi thanks for the reply. I hope that over the summer i'll be able to focus more time in getting deeper into Java. This project however needs to be complete by tuesday and this is the last piece of the puzzle before the app is ready to be demonstrated. This is in android studio. Do you think you could team view me and help fix the problem? it'd be greatly appreciated and would take a lot of stress off.

Really weird, I have a uni assignment (team project actually) that's an android app that interacts with a database and uses the Google Maps API etc. and it's also due to be demonstrated on Tuesday haha... I wish you luck and if you need any help then PM me!