簡體   English   中英

我們如何使用Google融合的位置服務獲取我們Android設備的准確位置?

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

我要求每15秒更新一次位置,但是每次位置更改時,即使設備位於同一位置也是如此。

誰能幫我這個忙,讓我知道確切的位置,至少是最大概的位置,這樣我就可以計算出旅行時的距離,並長時間站在同一地方等待時間。

您可以檢查位置的准確性。 這定義了您接收到的位置的准確性,您可以基於該位置過濾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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM