0

I tried to set the google map on fragment , but i found when i open it , it showed the location on Africa usually.

Why ? I though my code is correct ,it showed the correct location sometimes after all.

Some one can tell me why?

google map xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".PageFragment.TrafficInformation">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            tools:context=".MapLocationActivity" />

</LinearLayout>

my location set on this function:

 @Override
    public void onLocationChanged(Location location) {
        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }

        //Place current location marker  , i set it over here
        LatLng latLng = new LatLng(22.751262, 121.140801);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);

        //move map camera
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(16));

        //optionally, stop location updates if only current location is needed
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }

I referenced How can I show current location on a Google Map on Android Marshmallow?

any help would be grateful,thanks.

Community
  • 1
  • 1
Morton
  • 5,380
  • 18
  • 63
  • 118
  • Maybe because the location wasn't change. You need to request a location update first so this event will trigger. And if want the map to start on some place call movecamera on OnReady. – Henrique César Madeira Jan 26 '17 at 03:00
  • Why aren't you using the `Location` that is passed into the `onLocationChanged()` method override? – Daniel Nugent Jan 26 '17 at 03:08
  • @HenriqueCésarMadeira , so maybe i should set the movecamera funtion to OnReady ? – Morton Jan 26 '17 at 03:18
  • @DanielNugent , you mean 'LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());' ? Because I just want to set the location by myself. – Morton Jan 26 '17 at 03:19
  • Are you using the exact code from the answer in the link you posted? For me, the location comes up immediately, but it really depends on where you are. It will always try to use the Network Location first, and usually it can get your location from the WiFi access points around. It will use the GPS radio if it can't get a Network Location. If it's relying on GPS, you will need to go outside and walk around for the location updates to happen. – Daniel Nugent Jan 26 '17 at 03:27
  • It shows the correct location sometimes , i open the app indoors. So if the tester is indoors , it may doesn't work right? thanks @DanielNugent , you teach me some point. – Morton Jan 26 '17 at 03:48
  • But when my latitude and longitude is stationary , is't depend on network or GPS ? – Morton Jan 26 '17 at 03:54
  • I found the issue , if i use AsyncTask to get my latitude and longitude , i need make sure AsyncTask get complete before onMapReay. – Morton Jan 26 '17 at 06:25

0 Answers0