Par 32
Par 32
Program
activity_main.xml MainActivity.java
<?xml version="1.0" encoding="utf-8"?> package com.example.exp32_1;
<fragment import androidx.core.app.ActivityCompat;
xmlns:android="http://schemas.android.com/apk/res/and import androidx.fragment.app.FragmentActivity;
roid" import android.Manifest;
xmlns:map="http://schemas.android.com/apk/res- import android.content.pm.PackageManager;
auto" import android.graphics.Color;
xmlns:tools="http://schemas.android.com/tools" import android.location.Location;
android:id="@+id/map" import android.os.Bundle;
import com.google.android.gms.location.*;
android:name="com.google.android.gms.maps.Support import com.google.android.gms.maps.*;
MapFragment" import com.google.android.gms.maps.model.*;
android:layout_width="match_parent" import com.google.android.gms.tasks.*;
android:layout_height="match_parent"
tools:context=".MapsActivity" /> public class MapsActivity extends FragmentActivity
implements OnMapReadyCallback {
private GoogleMap mMap;
FusedLocationProviderClient locationClient;
Location currentLocation;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
locationClient =
LocationServices.getFusedLocationProviderClient(th
is);
fetchLocation();
SupportMapFragment mapFragment =
(SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
void fetchLocation() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATIO
N) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCA
TION}, 100);
return;
}
Task<Location> task =
locationClient.getLastLocation();
task.addOnSuccessListener(new
OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
currentLocation = location;
mMap.moveCamera(CameraUpdateFactory.newLatL
ngZoom(vvp, 15));
}
}
}
});
}
@Override
public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;
// Add markers
mMap.addMarker(new
MarkerOptions().position(newYork).title("New
York"));
mMap.addMarker(new
MarkerOptions().position(losAngeles).title("Los
Angeles"));
mMap.moveCamera(CameraUpdateFactory.newLatL
ngZoom(new LatLng(37.0, -96.0), 4));