2015-04-29 33 views
0

我正在每15秒申请位置更新,但每次位置发生变化时,即使设备位于同一位置。我们如何使用谷歌融合定位服务获得我们Android设备的准确位置?

任何人都可以帮助我,让我知道确切的位置,至少最接近的位置,这样我就可以在旅行时计算距离,并在相同的地方长时间等待时间。

+2

您可以检查位置的准确性。这定义了你接收到的位置有多准确,基于你可以过滤位置http://developer.android.com/reference/android/location/Location.html#getAccuracy() – Manish

+0

@Manish这应该是答案,因为它可能是OP正在寻找的点,或者他可能觉得它有用! –

+0

@PareshMayani完成将它添加为答案 – Manish

回答

0

你可以检查位置的准确性。这定义了您接收的位置的准确度,基于您可以过滤位置http://developer.android.com/reference/android/location/Location.html#getAccuracy()

+0

谢谢,但我也使用了准确性检查,它始终显示10.0 mtrs。我有点困惑,如何计算使用这个。 –

+0

您在此获得的值10表示用户位于10米范围内,中心位置为纬度和长度位置对象。通过这种方式,您可以非常准确地过滤出位置。从api文档“我们将精度定义为68%置信度的半径,换句话说,如果您绘制一个以该位置的经度和纬度为中心并且半径等于精度的圆,则有68%的概率真正的位置在圈内。“ – Manish

+0

我也同意这里的准确性。我经常收到不好的位置更新,所以我的服务时间长达几分钟,可能几小时没有位置更新。 –

0
1 Create a layout file named layout_map having a fragment to render map 

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    <com.google.android.gms.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/mapView" /> 
--> 
<!-- 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</RelativeLayout> 
--> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

2. Generate the mapkey and add it to in the manifest file 

    <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyBYc4fMCweLC4w77_hRctOktAt2bqLmnyg" /> 
3. Added metatdata tag for googleplay service 

    <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

4. Add the map,internet,corselocation permissions 

    <uses-permission android:name="com.smartdurg.fragment.map.permission.MAPS_RECEIVE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

5. Create an activity named as MapActivtivity 

package com.smartcity.bilaspur; 

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Address; 
import android.location.Geocoder; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 
import com.google.android.gms.maps.model.LatLng; 
public class MapActivity extends Activity { 

    String latitude,longitude,address,city,country; 
    LatLng defaultLatLng; 
    Context context; 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.map_activity); 
      try { 
       context = this; 
     Bundle extras = getIntent().getExtras(); 
     if (extras != null) 
     { 
      latitude = extras.getString("lat"); 
      longitude = extras.getString("lng"); 
      Log.e("MAP", "latitude? "+latitude); 
      Log.e("MAP", "longitude?"+longitude); 
     } 
     defaultLatLng = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude)); 

     Geocoder geocoder; 
      List<Address> addresses ; 
      geocoder = new Geocoder(this, Locale.getDefault()); 

      addresses = geocoder.getFromLocation(Double.valueOf(latitude), Double.valueOf(longitude), 1); 

      Log.v("addresses",">>>>>>>>>>>>>>>>"+addresses); 
      if (addresses.size()<=0) 
      { 
       Toast.makeText(this, "InValid Location",2).show(); 
       finish(); 
      } 
      else 
      { 
       address = addresses.get(0).getAddressLine(0); 
       city = addresses.get(0).getAddressLine(1); 
       country = addresses.get(0).getAddressLine(2); 
      String geoUri = "http://maps.google.com/maps?q=loc:" + latitude + "," + longitude + " (" + "GOT IT" + ")"; 

      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri)); 
      context.startActivity(intent); 
      } 
     } 

      catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 

In this case I have found the latitude & longitude form the webservice, you can find a code to get exact location from below link also with current latitude & longitude. 
http://stackoverflow.com/questions/17519198/how-to-get-the-current-location-latitude-and-longitude-in-android 
相关问题