Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nus.app1;
- import android.Manifest;
- import android.app.Activity;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.ContextCompat;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.MapFragment;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- public class GoogleMapActivity extends Activity implements OnMapReadyCallback, LocationListener
- {
- private static final int PERMISSION_REQUEST_CODE1 = 101;
- private GoogleMap map;
- Button btncurrentlocation,btnlastknownlocation;
- LocationManager locationManager;
- public void onCreate(Bundle b)
- {
- super.onCreate(b);
- setContentView(R.layout.googlemaplayout);
- MapFragment mapFragment = (MapFragment) getFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- btncurrentlocation=findViewById(R.id.btncurrentlocation);
- btnlastknownlocation=findViewById(R.id.btnlastknownlocation);
- btncurrentlocation.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- getCurrentLocation();
- }
- });
- btnlastknownlocation.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- }
- });
- }
- public void getCurrentLocation()
- {
- if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
- {
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE1);
- }
- else
- {
- locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
- locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this);
- }
- }
- public void onRequestPermissionsResult(int requestcode,String[] permissions,int[] grantresults)
- {
- if(requestcode==PERMISSION_REQUEST_CODE1)
- {
- if(grantresults.length>0 && grantresults[0]==PackageManager.PERMISSION_GRANTED)
- {
- getCurrentLocation();
- }
- }
- }
- //https://pastebin.com/3ne9n7Qq
- @Override
- public void onMapReady(GoogleMap googleMap) {
- map = googleMap;
- LatLng NUS = new LatLng(1.295150, 103.773638);
- map.addMarker(new MarkerOptions().position(NUS).title("NUS"));
- map.moveCamera(CameraUpdateFactory.newLatLng(NUS));
- }
- @Override
- public void onLocationChanged(Location location) {
- locationManager.removeUpdates(this);
- LatLng currentlocation=new LatLng(location.getLatitude(),location.getLongitude());
- map.addMarker(new MarkerOptions().position(currentlocation).title("Current Location"));
- map.moveCamera(CameraUpdateFactory.newLatLng(currentlocation));
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- }
- @Override
- public void onProviderDisabled(String provider) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement