简体   繁体   English

我们如何使用Google融合的位置服务获取我们Android设备的准确位置?

[英]How can we get an accurate Location of our Android Device, using google fused location service?

I am requesting location updates in every 15 sec, but each time the location gets changed, even if the device is on the same position. 我要求每15秒更新一次位置,但是每次位置更改时,即使设备位于同一位置也是如此。

Can anyone help me on this, to make me know exact location, atleast most approximate location, so that I can Calculate the Distance while traveling, and wait time while standing on the same place for a long time. 谁能帮我这个忙,让我知道确切的位置,至少是最大概的位置,这样我就可以计算出旅行时的距离,并长时间站在同一地方等待时间。

you can check the accuracy of the location. 您可以检查位置的准确性。 This defines how accurate is the location you received, based on that you can filter the locations http://developer.android.com/reference/android/location/Location.html#getAccuracy() 这定义了您接收到的位置的准确性,您可以基于该位置过滤http://developer.android.com/reference/android/location/Location.html#getAccuracy()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中使用Google API(融合位置)获取准确的GPS位置? - How to get accurate GPS location Using google API(Fused Location) in android? 我们如何通过Android设备获得准确的位置? - How accurate location can we get through an android device? 如何使用融合位置正确获取准确位置 - How to properly get an accurate location using fused location 我想使用 google fused location api 获得准确的位置? - I want to get accurate location with google fused location api? 使用Google API的背景位置更新 - 融合位置提供程序不准确 - Background Location updates using Google API - Fused Location Provider not accurate 如何使用网络在 android 中使用 Fused Location Provider 获取位置? - How to get location using Fused Location Provider in android using Network? 当Google Play服务在应用和设备上不同时,使用融合位置获取CurrentLocation 0 - Getting CurrentLocation 0 using fused location when google play service is different in app and device 无法使用Google的融合位置API获取位置信息 - Unable to get location using Google's fused location API 我们可以离线使用Google的融合位置服务吗? - Can we use Google's Fused Location services in offline mode? 无法在Android设备上获取准确的位置 - Not able to get accurate location on android device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM