2016-10-23 25 views
0

你好,我有一個關於位置獲取經度和緯度的問題。 它在我的數據庫上始終爲0.0。Android MapView getlongitude和getlatitude返回0.0排除請求

它採用凌空傳遞的LNGS和拉特變量,但是當它到達值爲0.0

變量拉特和LNGS應該在onLocationChanged()函數

這裏進行了更新一些的我的代碼

Private double lats; 
Private double lngs; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    // Inflate the layout for this fragment 
    View myinflate = inflater.inflate(R.layout.fragment_home, container, false); 
    final TextView usr = (TextView)myinflate.findViewById(R.id.user); 



    Bundle extras = getActivity().getIntent().getExtras(); 
    usrnme = extras.getString("user"); 


    requestQueue = Volley.newRequestQueue(getActivity()); 


    locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE); 

    listener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      lats=location.getLatitude(); 
      lngs=location.getLongitude(); 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 


    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 

      ActivityCompat.requestPermissions(getActivity(),PERMISSIONS_LOCATION,REQUEST_LOCATION); 

     } 

    } 
    else 
    { 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener); 
    } 





      StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

       } 

      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 


       } 
      }) { 

       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 
        Map<String, String> parameters = new HashMap<String, String>(); 
        parameters.put("usr", usrnme); 
        parameters.put("lat", Double.toString(lats)); 
        parameters.put("lng", Double.toString(lngs)); 

        return parameters; 
       } 
      }; 
      requestQueue.add(request); 

回答

1

我認爲你正在交付lats和longs服務器提前一點。相反,在從locationManager偵聽器調用onLocationChanged方法後立即發送位置。

Private double lats; 
Private double lngs; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    // Inflate the layout for this fragment 
    View myinflate = inflater.inflate(R.layout.fragment_home, container, false); 
    final TextView usr = (TextView)myinflate.findViewById(R.id.user); 



    Bundle extras = getActivity().getIntent().getExtras(); 
    usrnme = extras.getString("user"); 


    requestQueue = Volley.newRequestQueue(getActivity()); 


    locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE); 

    listener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      lats=location.getLatitude(); 
      lngs=location.getLongitude(); 

      StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

       } 

      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 


       } 
      }) { 

       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 
        Map<String, String> parameters = new HashMap<String, String>(); 
        parameters.put("usr", usrnme); 
        parameters.put("lat", Double.toString(lats)); 
        parameters.put("lng", Double.toString(lngs)); 

        return parameters; 
       } 
      }; 
      requestQueue.add(request); 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 


    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 

      ActivityCompat.requestPermissions(getActivity(),PERMISSIONS_LOCATION,REQUEST_LOCATION); 

     } 

    } 
    else 
    { 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener); 
    } 
+0

謝謝!有效! lats和lng現在在我的數據庫中!然而,當我試圖在地圖上的標記中使用lats和lngs變量時,它會將我發送到非洲!大聲笑這不等於相同的lats和lngs變量發送..任何想法? – Duckbenok

+1

從'onLocationChanged',你可以把lats和lngs的值記錄下來,然後把這個值放在www.maps.google.com看看它實際上在哪裏?如果它仍然引領你到非洲,那麼你從服務器獲得的位置是真正由你的應用程序給出的。 – fluffyBatman

+0

這很奇怪,當我記錄lat和lngs在onLocationChanged中的值時,這些值是正確的,但是當我在標記區域外記錄外部值時,值爲0.0和0.0,儘管lat和lng被聲明在最上面, lat和lngs的值應該改變了,但爲什麼它返回0?真奇怪 – Duckbenok