Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.abc.psproject;
- 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.support.v4.app.ActivityCompat;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.content.ContextCompat;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- 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;
- /**
- * Created by selva on 12/20/2017.
- */
- public class googlemapactivity extends FragmentActivity implements OnMapReadyCallback,LocationListener
- {
- private GoogleMap map;
- private TextView tcurrentlocation;
- private TextView tlastknownlocation;
- private Button btncurrentlocation;
- private Button btnlastknownlocation;
- private LocationManager locationmanager;
- public void onCreate(Bundle b)
- {
- super.onCreate(b);
- setContentView(R.layout.googlemaplayout);
- MapFragment mapFragment=(MapFragment)getFragmentManager().findFragmentById(R.id.mapfragment);
- mapFragment.getMapAsync(this);
- tcurrentlocation=findViewById(R.id.tcurrentlocation);
- tlastknownlocation=findViewById(R.id.tlastknownlocation);
- btncurrentlocation=findViewById(R.id.btncurrentlocation);
- btncurrentlocation.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- tcurrentlocation.setText("Loading Current Location ....");
- getcurrentlocation();;
- }
- });
- btnlastknownlocation=findViewById(R.id.btnlastknownlocation);
- btnlastknownlocation.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- tlastknownlocation.setText("Loading Last Known Location....");
- getlastknownlocation();
- }
- });
- }
- @Override
- public void onMapReady(GoogleMap googleMap)
- {
- map=googleMap;
- LatLng kismec=new LatLng(3.855542, 103.320440);
- map.addMarker(new MarkerOptions().position(kismec).title("Pahang Skills"));
- map.moveCamera(CameraUpdateFactory.newLatLng(kismec));
- }
- 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,Manifest.permission.ACCESS_FINE_LOCATION},1);
- }
- else
- {
- locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
- locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,googlemapactivity.this);
- }
- }
- public void getlastknownlocation()
- {
- if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
- {
- ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.ACCESS_COARSE_LOCATION},2);
- }
- else
- {
- locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
- Location location=locationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- tlastknownlocation.setText(location.getLatitude()+","+location.getLongitude());
- LatLng lastknownlocation=new LatLng(location.getLatitude(),location.getLongitude());
- map.addMarker(new MarkerOptions().position(lastknownlocation).title("Last Known Location"));
- map.moveCamera(CameraUpdateFactory.newLatLng(lastknownlocation));
- }
- }
- public void onRequestPermissionsResult(int requestcode,String[] permissions,int[] grantresults)
- {
- if(requestcode==1)
- {
- if(grantresults.length>0 && grantresults[0]==PackageManager.PERMISSION_GRANTED)
- {
- getcurrentlocation();
- }
- else
- {
- Toast.makeText(getApplicationContext(),"You denied location permission",Toast.LENGTH_LONG).show();
- }
- }
- else if(requestcode==2)
- {
- if(grantresults.length>0 && grantresults[0]==PackageManager.PERMISSION_GRANTED)
- {
- getlastknownlocation();
- }
- else
- {
- Toast.makeText(getApplicationContext(),"You denied location permission",Toast.LENGTH_LONG).show();
- }
- }
- }
- @Override
- public void onLocationChanged(Location location)
- {
- tcurrentlocation.setText(location.getLatitude()+","+location.getLongitude());
- 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 s, int i, Bundle bundle)
- {}
- @Override
- public void onProviderEnabled(String s)
- {}
- @Override
- public void onProviderDisabled(String s)
- {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement