[英]How Can I get Longitude and Latitude and put it into JSON link?
我一直在嘗試使用LocationListener捕獲經度和緯度,並將其強制轉換為int以放入JSON鏈接,以便可以使用當前用戶位置的數據。 嘗試啟動它時,我的應用程序崩潰了,但是我可以獲取經度和緯度坐標。 我已經將我的應用程序分解為僅僅能夠從一個活動中獲取經度和緯度,並試圖在另一個活動中獲取這些值,然后將我的JSON活動放入其中。 有什么我想念的嗎?
表現
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="viaan.carl.mytest" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
主要活動
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener {
LocationManager locationmanager;
myGPS gps = new myGPS(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria cri=new Criteria();
String provider=locationmanager.getBestProvider(cri,false);
if(provider!=null & !provider.equals(""))
{
Location location=locationmanager.getLastKnownLocation(provider);
locationmanager.requestLocationUpdates(provider,2000,1,this);
if(location!=null)
{
onLocationChanged(location);
}
else{
Toast.makeText(getApplicationContext(),"location not found",Toast.LENGTH_LONG ).show();
}
}
else
{
Toast.makeText(getApplicationContext(),"Provider is null",Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
int lat;
gps.setLat((int) location.getLatitude());
int lng;
gps.setLng((int) location.getLongitude());
//TextView textView2=(TextView)findViewById(R.id.textview2);
//TextView textView3=(TextView)findViewById(R.id.textview3);
//textView2.setText("Latitude"+location.getLatitude());
//textView3.setText("Longitude" + location.getLongitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
吸氣與塞特課程
import android.app.Activity;
import android.content.SharedPreferences;
public class myGPS {
private int lat;
private int lng;
SharedPreferences prefs;
public myGPS(Activity activity) {
prefs = activity.getPreferences(Activity.MODE_PRIVATE);
}
void setLng(int lng) {
this.lng = lng;
}
int getLat() {
return lat;
}
int getLng() {
return lng;
}
void setLat(int lat) {
this.lat = lat;
}
}
JSON將使用的新類(當前僅嘗試顯示數據)
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class LocationDisplay extends Fragment {
TextView textView2;
TextView textView3;
myGPS gps;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_wind, container, false);
textView2 = (TextView)rootView.findViewById(R.id.textview2);
textView3 = (TextView)rootView.findViewById(R.id.textview3);
return rootView;
}
public LocationDisplay () {
textView2.setText("Latitude: " + gps.getLat());
textView3.setText("Longitude: " + gps.getLng());
}
}
logcat的
06-05 11:36:52.499 1943-1943/? D/dalvikvm﹕ Late-enabling CheckJNI
06-05 11:36:52.567 1943-1943/viaan.carl.mytest E/Trace﹕ error opening trace file: No such file or directory (2)
06-05 11:36:52.575 1943-1943/viaan.carl.mytest D/AndroidRuntime﹕ Shutting down VM
06-05 11:36:52.575 1943-1943/viaan.carl.mytest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa619f908)
06-05 11:36:52.583 1943-1943/viaan.carl.mytest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{viaan.carl.mytest/viaan.carl.mytest.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
at android.app.Activity.getLocalClassName(Activity.java:4415)
at android.app.Activity.getPreferences(Activity.java:4449)
at viaan.carl.mytest.myGPS.<init>(myGPS.java:16)
at viaan.carl.mytest.MainActivity.<init>(MainActivity.java:29)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
您以舊方式獲取Location
。 Google為此IO發布了新的Places API 。 您還可以通過以下方式獲取用戶在Android上的當前位置 :
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
// this is your Location
Log.i(TAG, String.format("Place '%s' has likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
}
likelyPlaces.release();
}
});
如果需要當前位置,則不必進行API調用。 這是您的入門方法 。 您可以從“ 位置”對象獲取經度/緯度。
double latitude = location.getLatitude();
double longitude = location.getLongitude();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.