Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Didn’t know how to get the current location and show nearby car services(workshops)

Posted on November 11, 2022 By

Solution 1 :

As

map.getMyLocation()

is depracated you can try this:

   public Location getLocation() {
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (lastKnownLocationGPS != null) {
                return lastKnownLocationGPS;
            } else {
                return locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            }
        } else {
            return null;
        }
    }

And dont forget about permissions (runtime and in manifest).

Problem :

I have a google maps in one fragment in my app. How can I get the current location and show nearby car services?

package com.example.easyauto;

import android.location.Location;
import android.os.Bundle;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class HartaFragment extends Fragment implements OnMapReadyCallback {

    View view;   // for the view
    GoogleMap map;   // for the GoogleMaps
    MapView mapView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_harta, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mapView = view.findViewById(R.id.mapsView);

                if(mapView != null){
                    mapView.onCreate(null);
                    mapView.onResume();
                    mapView.getMapAsync(this);

                }

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(getContext());
        map = googleMap;  // show the maps ok 
    }
} 

This is the code I wrote for now. It all works fine. Do I need also to put tha map_fragment.xml here for more details?
I also added the meta-data for google maps and the implementation ‘com.google.android.gms:play-services-maps:16.1.0’,

Comments

Comment posted by Codelab

Welcome to StackOverflow. I would recommend to be more concise in your questions to be easier to answer for other people. You aren’t quite asking a question, but asking for someone else to do it for you. I would hardly recommend to follow some sort of

READ  [FIXED] java - What causes Android Studio to flag the FragmentManager as deprecated?
Powered by Inline Related Posts
Android Tags:android, android-studio, google-maps-android-api-2, java, maps

Post navigation

Previous Post: [FIXED] Xamarin.forms – Trouble behaviour between ListView & keyboard
Next Post: [FIXED] android – two elements in linear layout are packed to center instead of to the sides

Related Posts

[FIXED] Integrate OpenCV with Android Project Android
[FIXED] android – Jetpack compose handle escape or back button? Android
[FIXED] android – Compound query not giving correct results [Firestore] Android
[FIXED] xamarin.forms – create xamarin process never end Android
[FIXED] Is there any way to override the back stack button to go back to a screen with its index in jetpack compose? Android
[FIXED] android – After updating variable in my view Model my fragment editText does not update Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme