//location dependncy
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'com.google.android.gms:play-services-maps:18.0.1'
Menifest permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
android:theme="@style/Theme.WFMPartner">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
package com.trickpiwal.wfmpartner;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.common.internal.service.Common;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.trickpiwal.wfmpartner.databinding.FragmentMapshowBinding;
import java.util.ArrayList;
public class MapshowFragment extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private ArrayList<Marker> myMarkers;
private Marker driverMarker;
private Marker passengerMarker;
private FragmentMapshowBinding mBinding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding=FragmentMapshowBinding.inflate(getLayoutInflater());
setContentView(mBinding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
sartrtShowingMap();
}
private void sartrtShowingMap() {
LatLng driverlocation = new LatLng(ModelSaveLcation.drverLocation.getLatitude(),
ModelSaveLcation.drverLocation.getLongitude());
LatLng pLocation= new LatLng( ModelSaveLcation.passngerLocation.getLatitude(),
ModelSaveLcation.passngerLocation.getLongitude());
LatLngBounds.Builder builder = new LatLngBounds.Builder();
if(driverMarker!=null){
driverMarker.remove();
}
driverMarker = mMap.addMarker(new MarkerOptions().position(driverlocation).title("Driver Location"));
driverMarker.setAnchor(0.5f,0.7f);
driverMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.driver));
driverMarker.setRotation(ModelSaveLcation.drverLocation.getBearing());
if(passengerMarker!=null){
passengerMarker.remove();
}
passengerMarker = mMap.addMarker(new MarkerOptions().position(pLocation));
passengerMarker.setAnchor(0.5f,0.5f);
passengerMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.passengericon));
// passengerMarker.setRotation(ModelSaveLcation.drverLocation.getBearing());
myMarkers = new ArrayList<>();
myMarkers.clear();
myMarkers.add(driverMarker);
myMarkers.add(passengerMarker);
for (Marker marker : myMarkers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 60);
mMap.animateCamera(cameraUpdate);
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
No comments:
Post a Comment