0% found this document useful (0 votes)
7 views2 pages

Map

The document defines a MainActivity class that extends AppCompatActivity and implements OnMapReadyCallback. It uses the FusedLocationProviderClient to get the device's location and displays it on a map with a marker.

Uploaded by

html backup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Map

The document defines a MainActivity class that extends AppCompatActivity and implements OnMapReadyCallback. It uses the FusedLocationProviderClient to get the device's location and displays it on a map with a marker.

Uploaded by

html backup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import androidx.appcompat.app.

AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import com.google.android.gms.location.*;
import com.google.android.gms.tasks.*;

public class MainAcivity extend AppCompatActivity implements OnMapReadyCallback{

Location loc ;
FusedLocationProviderClient flpc;
int REQ_Code = 101;

protected void onCreate(Bundle s){

super.onCreate(s);
setContentView(R.layout.act_map);

flpc = LocationServices.getFusedLocationProviderClient(this);

fetchLocation();

public void fetchLocation(){

if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED){

ActivityCompat.requestPermissions(this,Manifest.permission.ACCESS_FINE_LOCATION,REQ
_CODE);

flpc.getLastLocation(new OnSucessListener(){

public onSucess(Location location){


loc = location;
SupportMapFragment mf = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);

mf.getMapAsync(this);

}
public void onMapReady(GoogleMap gm){

if(loc != null){
LatLng latLng = new LatLng(loc.getLatitude(),loc.getLongitude());

gm.animateCamera(CameraUpdateFactory.newLatLng(latLng));

gm.addMarker(new MarkerOptions().position(latLng).title("Your
here"));

}
}

public void onRequestPermissionResult(int ReqCode, String[] permissions, int


gResults){

super.onRequestPermissionResult(ReqCode,permissions,gResults);

if(ReqCode == REQ_CODE && gResults[0] ==


PackageManager.PERMISSION_GRANTED){

fetchLocation();
}

You might also like