1

グーグルAPIから緯度を取得する方法と私はそれが完璧に機能するいくつかのコードを持っていますが、時々それは私たちに、私がインドにいるときでさえ英国の価値を与えています。

String surl = "http://mobilemaps.clients.google.com/glm/mmap";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(surl);
httppost.setEntity(new MyCellIDRequestEntity(shortcid, lac));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
DataInputStream dis = new DataInputStream(entity.getContent());

// Read some prior data
dis.readShort();
dis.readByte();
// Read the error-code
int errorCode = dis.readInt();
System.err.println("Error Code iss::" + errorCode);
int api = myPrefs2.getInt("api", 0);
if (errorCode == 0) 
{
    lat = (double) dis.readInt() / 1000000D;
    lng = (double) dis.readInt() / 1000000D;
    System.err.println("lattitude::" + Double.toString(lng) + "::long::" + Double.toString(lat));
} else {
    // System.err.println("Wrong Error Code");
}
} catch (Exception e) {
}
4

2 に答える 2

0

これを試してみてください -

関数の呼び出し アクティビティから :

String location_name = getmylocation(latitude+","+longitude)

getMyLocation()メソッドは、現在地の文字列を提供します -

public String getmylocation(String value)
{           
      JSONObject json = JSONfunctions.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng="+value+"&sensor=true");
      try{
        JSONArray  georesult = json.getJSONArray("results");
            JSONObject e = georesult.getJSONObject(0);

            String result = e.getString("address_components");
            JSONArray jArray1 = new JSONArray(result);
            JSONObject json_data=jArray1.getJSONObject(3);

            loc = json_data.getString("long_name");

      }catch(JSONException e)
      {
        e.printStackTrace();
      }    
    return loc;
}   

JSONfunctions.java

package com.sample.getcurrentlocation;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url){
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

      //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{

            jArray = new JSONObject(result);            
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return jArray;
    }
}

を使用して場所を簡単に確認できます。String location_nameまた、必要な権限を追加することを忘れないでください -

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
于 2012-06-08T06:35:16.460 に答える
0

ユーザーの場所の完全なアドレスを取得するのに役立つリンクを通過するだけです.

于 2012-06-08T05:50:56.973 に答える