2010-12-09 71 views

回答

1

主題Obtaining user location結束於包括使用Android中的內置功能獲取位置。

報告還指出這一點:

網絡位置提供商提供了良好的位置數據,而無需使用GPS。

和:

限制一組提供商

取決於在使用你的應用環境或期望的精確程度,你可以選擇只使用網絡位置提供商或只有GPS,而不是兩者。只與其中一項服務進行交互,可能會降低電池使用的準確性。

0

爲什麼你想用ip地址找到位置? IP地址只能提供國家/地區詳細信息的位置信息,而且您依賴的是具有IP地理位置的Web服務,這種服務不太可靠,只有在您訪問互聯網時才能使用。另外,如果您要連接到應用程序中的服務器,那麼您可以讓服務器執行此操作,速度會更快。

你有設備上的GPS,可以根據經緯度給你實時的用戶位置。你可以按照鏈接在Joakim的回答

0

如果你想獲得移動位置,你需要第一個提供者在我的情況下,我使用谷歌API來從你的手機獲取GPS位置,你需要從API獲取令牌下面的教程解釋瞭如何從手機獲取位置真的很好。

http://blog.teamtreehouse.com/beginners-guide-location-android 

並從android開發人員網絡,如果你雖然有疑慮。

http://developer.android.com/training/location/retrieve-current.html 
http://developer.android.com/training/location/receive-location-updates.html 

這是最重要的,我在開發我從一個片段得到的位置和我用的片段中的任何位置活動,其獲得的位置需要在我的Android應用程序的位置..

使用回調來請求移動電話的位置。

重要的,你需要的位置和其他工作的許可......

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<!-- The following two permissions are not required to use 
    Google Maps Android API v2, but are recommended. --> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

<uses-feature android:glEsVersion="0x00020000" android:required="true"/> 

XML的活動必須有片段:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<fragment android:name="es.urjc.mov.javsan.fichas.LocationFragment" 
    android:id="@+id/location_fragment" 
    android:layout_height="0dip" 
    android:layout_weight="0" 
    android:layout_width="match_parent" /> 

</LinearLayout> 

片段代碼:

package es.urjc.mov.javsan.fichas; 

import android.Manifest; 
import android.app.Fragment; 
import android.content.IntentSender; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 

public class LocationFragment extends Fragment implements 
     com.google.android.gms.location.LocationListener, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener { 

private static final String TAG = LocationFragment.class.getSimpleName(); 

private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
private android.location.Location currentLocation; 

private View fragmentLayout; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    super.onCreateView(inflater, container, savedInstanceState); 
    fragmentLayout = inflater.inflate(R.layout.location_fragment, container, false); 

    buildLocation(); 
    return fragmentLayout; 
} 


@Override 
public void onStart() { 
    if (!mGoogleApiClient.isConnected()) { 
     mGoogleApiClient.connect(); 
    } 
    super.onStart(); 
} 

@Override 
public void onResume() { 
    if (!mGoogleApiClient.isConnected()) { 
     mGoogleApiClient.connect(); 
    } 
    super.onResume(); 
} 

@Override 
public void onStop() { 
    if (mGoogleApiClient.isConnected()) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     mGoogleApiClient.disconnect(); 
    } 
    super.onStop(); 
} 


@Override 
public void onConnected(Bundle bundle) { 
    if (isDenyLocation()) { 
     requestAllowLocation(); 
     return; 
    } 

    android.location.Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location != null) { 
     handleNewLocation(location); 
    } else { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.e(TAG, String.format("%s\n", "Connection suspended please reconnect...")); 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Log.v(TAG, String.format("Connection failed : %s", connectionResult.toString())); 

    if (!connectionResult.hasResolution()) { 
     Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
     return; 
    } 

    try { 
     // Start an Activity that tries to resolve the error 
     connectionResult.startResolutionForResult(getActivity(), CONNECTION_FAILURE_RESOLUTION_REQUEST); 
    } catch (IntentSender.SendIntentException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    handleNewLocation(location); 
} 

private void buildLocation() { 

    if (mGoogleApiClient == null) { 
     // Create the GoogleApiClient Object. 
     mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .addApi(AppIndex.API).build(); 
    } 

    if (mLocationRequest == null) { 
     // Create the LocationRequest Object. 
     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); // 1 second, in milliseconds 
    } 

} 

private void handleNewLocation(Location location) { 
    currentLocation = location; 
    Log.e(TAG, String.format("Current Location : %f, %f", 
      currentLocation.getLongitude(), currentLocation.getLatitude())); 
} 

我希望這個幫助!

Javi,

相關問題