/ / Android स्टूडियो मैप्स डिफ़ॉल्ट व्यू ऐप को फिर से चालू करता है - जावा, एंड्रॉइड, गूगल-मैप्स, गूगल-मैप्स-एपीआई -3

एंड्रॉइड स्टूडियो मैप रीसेट डिफ़ॉल्ट ऐप को फिर से शुरू करता है - जावा, एंड्रॉइड, गूगल-मैप्स, गूगल-मैप्स-एपीआई -3

मुझे यकीन नहीं है कि क्या हो रहा है, लेकिन जब मैं अपना काम करता हूंपहली बार ऐप, कभी-कभी मैप मेरे मार्कर पर ज़ूम करता है। हालाँकि, कभी-कभी, यह केवल मेरे मार्कर के बिना डिफ़ॉल्ट ज़ूम आउट स्थिति दिखाएगा। मुझे आशा है कि कोई मेरे कोड को देख सकता है और देख सकता है कि क्या कुछ गलत हुआ?

कभी-कभी यदि मैं अपना ऐप पुनः आरंभ करता हूं, तो मार्कर तब दिखाई देगा।

public class MapFragment extends SupportMapFragment implements LocationListener,OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

private GoogleMap googleMap;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
Location location;
Marker locationMarker;
SharedPreferences sharedPreferences;
String firstName;
String lastName;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Important as without this, you will be loading a map only
getMapAsync(this);
sharedPreferences = getActivity().getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
firstName = sharedPreferences.getString("Firstname","");
lastName = sharedPreferences.getString("Lastname","");
}

protected synchronized void buildGoogleAPIClient(){
googleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {
LatLng currentLocation = new LatLng(location.getLatitude(),location.getLongitude());
Double latitude = currentLocation.latitude;
Double longitude = currentLocation.longitude;
this.location = location;
if(locationMarker!=null){
locationMarker.remove();
}
Log.d("Current Location",latitude + " " + longitude);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(currentLocation);
markerOptions.title(firstName + " " + lastName);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
locationMarker = googleMap.addMarker(markerOptions);
float zoomLevel = 18;
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,zoomLevel));

if(googleApiClient!=null){
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
}
}

@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
buildGoogleAPIClient();
this.googleMap.setMyLocationEnabled(true);
this.googleMap.getUiSettings().setMapToolbarEnabled(false);
}else{
checkLocationPermission();
}
}else{
buildGoogleAPIClient();
this.googleMap.setMyLocationEnabled(true);
this.googleMap.getUiSettings().setMapToolbarEnabled(false);
}
}

@Override
public void onPause() {
super.onPause();
if(googleApiClient!=null){
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
}
}

@Override
public void onConnected(@Nullable Bundle bundle) {
locationRequest = new LocationRequest();
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if(ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,locationRequest,this);
}
}

private void checkLocationPermission(){
if(ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
//Ask for permission again once the user sees the explanation
if(ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)){
new AlertDialog.Builder(getActivity())
.setTitle("Location Permission required")
.setMessage("This app requires location permissions in order to function")
.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSIONS_REQUEST_LOCATION);
}
})
.create()
.show();
}else{
//No explanation required. Just request for permissions
ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSIONS_REQUEST_LOCATION);
}
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case MY_PERMISSIONS_REQUEST_LOCATION: {
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
if(googleApiClient==null){
buildGoogleAPIClient();
}
if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
this.googleMap.setMyLocationEnabled(true);
this.googleMap.getUiSettings().setMapToolbarEnabled(false);
}
}else{
Toast.makeText(getActivity(),"Permission denied",Toast.LENGTH_LONG).show();
}
return;
}
}
}

उत्तर:

उत्तर № 1 के लिए 1

माफ़ करना! मुझे अंततः एहसास हुआ कि क्या हो रहा था। जब मैं कनेक्ट कर रहा था तो मैं वास्तव में मार्कर नहीं खींच रहा था।

उपाय : इन 2 लाइनों को ऑनकनेक्ट पर जोड़ें

String getLastLocation = ""+LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
updateLocation(getLastLocation);

मैंने अंतिम स्थान प्राप्त करने के लिए एक फ़ंक्शन बनाया

private void updateLocation(String getLastLocation){
getLastLocation = getLastLocation.replace("Location[fused","");
String[] splitLocation = getLastLocation.split("acc");
String[] splitLocationResult = splitLocation[0].split(",");
Double latitude = Double.parseDouble(splitLocationResult[0]);
Double longitude = Double.parseDouble(splitLocationResult[1]);
LatLng coordinates = new LatLng(latitude,longitude);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(coordinates,ZOOMLEVEL);
googleMap.moveCamera(cameraUpdate);
addMarkers(coordinates);
}