2010-12-09 6 views

답변

1

주제 Obtaining user location()은 Android에서 inbuilt 함수를 사용하여 위치를 얻는 방법을 다룹니다.

은 또한이 상태 :

네트워크 위치 제공자는 GPS를 사용하지 않고 좋은 위치 데이터를 제공합니다.

그리고 :

응용 프로그램이 사용되는 환경이나 정확성 원하는 수준에 따라 제공

의 집합을 제한, 당신은 단지 네트워크 위치 공급자를 사용하도록 선택할 수 있습니다 또는 GPS 대신 GPS를 사용합니다. 서비스 중 하나와 만 상호 작용하면 잠재적 인 정확성 비용으로 배터리 사용량이 줄어 듭니다.

0

왜 위치를 찾기 위해 ip 주소를 사용 하시겠습니까? IP 주소는 국가 정보만으로 위치 정보를 제공 할 수 있으며 인터넷 서비스가있는 경우에만 신뢰할 수있는 IP로 지리적 위치를 수행하는 웹 서비스에 의존합니다. 또한 앱의 서버에 연결하는 경우 서버가 훨씬 빠르게 수행 할 수 있습니다.

당신은 위도와 경도면에서 실시간 사용자 위치를 제공하는 GPS를 장비에 가지고 있습니다. 당신은 Joakim의 대답에있는 링크를 따라갈 수 있습니다

0

당신이 내 경우에 당신이 공급자를 필요로하는 모바일 위치를 얻으려면 Google API를 사용하여 휴대 전화 GPS 위치를 얻으려면 API에서 토큰을 가져와야합니다. 다음 튜토리얼은 휴대 전화에서 위치를 얻는 방법을 설명합니다.

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

그리고 당신은 비록 의심이있는 경우 안드로이드 개발자 웹에서

.

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

그리고 내 안드로이드 응용 프로그램에서 위치를 필요 ...

이는 내가 조각에서 위치를 얻을 수있어 개발에 나를 위해 가장 중요하고 내가 그 어디에서나 활동에 위치를 얻을 수있는 조각을 사용하여

콜백을 사용하여 휴대 전화의 위치를 ​​요청합니다.

<?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())); 
} 

I

이 중요 활동에

<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이 조각이 있어야합니다 ... 위치와 작업 다른 사람의 허락이 필요 이 도움이되기를 바랍니다! 당신은 안드로이드, 그런 식으로이 질문에 태그를해야