Techrens Alert – part 3

Techrens Alert – part 3

So now we move on to the next stage of this project. So what we have now is the longitude and latitude of our current location we need to be able to view it in a read able format.

So before we dive into it, I would encourage everyone to view Latlong.net this website takes longitude and latitude and gives you the address for those coordinates. Originally I was going to use that for my app, but then I realize that why not use Google maps like the the app I am replacing. So without further delay here is what I did.

Part 3

I opened up the content_test1.xml and added another button under the Check GPS button. I named the new button btnOpenLink and had the text display View Link.

After that I added the following code (look for blue highlights)

 

public class TestActivity1 extends AppCompatActivity implements LocationListener {

    LocationManager locationManager;
    String mprovider;
    Location location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test1);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        final Button button = (Button) findViewById(R.id.btnCheckGps);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                getLocation();
            }
        });


        final Button openLink = (Button) findViewById(R.id.btnOpenLink);
        openLink.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (location != null)
                    getUrl();
            }
        });

    }

    private void getUrl(){

        String myUrl = "http://maps.google.com/maps?q=" + location.getLatitude() + "," + location.getLongitude();
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myUrl));
        startActivity(browserIntent);

    }

    private void getLocation() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();

        mprovider = locationManager.getBestProvider(criteria, false);

        if (mprovider != null && !mprovider.equals("")) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            location = locationManager.getLastKnownLocation(mprovider);
            locationManager.requestLocationUpdates(mprovider, 15000, 1, this);

            if (location != null)
                onLocationChanged(location);
            else
                Toast.makeText(getBaseContext(), "No Location Provider Found Check Your Code", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_test_activity1, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onLocationChanged(Location location) {
        TextView message = (TextView) findViewById(R.id.txtMessage);


        message.setText("Current Longitude:" + location.getLongitude() +  "\n\nCurrent Latitude:" + location.getLatitude());

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }


}

 

The first thing I did was made the variable location a global variable so that I can access it in the getUrl method. The getUrl method retrieves the longitude and latitude then formats it into a URL. Once the URL is formed a new window is open using the default browser or Google maps. All of this happens when the user presses the View Link button.

And there you have it that concludes this part. If you are following along you can update your code with the code above. Otherwise you can test the current apk with the link below.

So there you have it another apk that you can test and try to break 🙂

So please let me know what your thoughts in the comment section below!

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *