2012-05-01 2 views
0

Geo Coding API에서 위도와 경도 위치를 가져오고 싶습니다. 다음 코드를 작성했습니다.Geo 코딩 API에서 lat와 lang을 가져 오겠습니까?

package com.appulento.mapsexample.pack; 
import android.graphics.Point; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.MotionEvent; 
import android.view.View; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.Projection; 
import com.mapsinfo.pack.DBAdapter; 

public class MapsMianClass extends MapActivity { 

    private MapController mapController; 
    private LocationManager locationManager; 
    private MapView mapView; 
    List<Overlay> listOfOverlays ; 
    private List mapOverlays; 
    private Projection projection; 
    private Geocoder geoCoder; 
    private MapController mc; 
    private GeoPoint gP; 
    private DBAdapter db; 

    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //here i am giving the Maps Geo coding API URL  

     Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false")); 
     startActivity(intent); 
     //starting the Intent 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    //default method of maps Activity. 
    }   
} 

맞습니까? URL에서 위도와 경도 값을 가져 오는 위 코드에 JSON을 어떻게 통합 할 수 있습니까?

답변

0

의 응답을 구문 분석이 코드

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.URI; 

public class MyActivity extends Activity { 
    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     AsyncTask<String, Void, Void> stringVoidVoidAsyncTask = new AsyncTask<String, Void, Void>() { 

      BufferedReader in; 

      @Override 
      protected Void doInBackground(String... strings) { 

       String url = ""; 
       if (strings.length > 0) { 
        url = strings[0]; 
       } else { 
        return null; 
       } 
       try { 
        HttpClient httpClient = new DefaultHttpClient();// Client 
        HttpGet getRequest = new HttpGet(); 

        getRequest.setURI(new URI(url)); 
        HttpResponse response = httpClient.execute(getRequest); 


        in = new BufferedReader 
          (new InputStreamReader(response.getEntity().getContent())); 
        StringBuffer sb = new StringBuffer(""); 
        String line = ""; 
        String NL = System.getProperty("line.separator"); 
        while ((line = in.readLine()) != null) { 
         sb.append(line + NL); 
        } 
        in.close(); 
        String page = sb.toString(); 
        JSONObject jsonObject = new JSONObject(page); 
        JSONArray jsonArray = (JSONArray) jsonObject.get("results"); 
        if (jsonArray.length() > 0) { 
         jsonObject = (JSONObject) jsonArray.get(0); 
         jsonObject = (JSONObject) jsonObject.get("geometry"); 
         JSONObject location = (JSONObject) jsonObject.get("location"); 
         Double lat = (Double) location.get("lat"); 
         Double lng = (Double) location.get("lng"); 
         System.out.println("lat - " + lat + " , lon - " + lng); 
        } 
        System.out.println(page); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } finally { 
        if (in != null) { 
         try { 
          in.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
       return null; 
      } 
     }; 
     stringVoidVoidAsyncTask.execute("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true"); 
    } 
} 

을 시도하고

<uses-permission android:name="android.permission.INTERNET"/> 

과의 인터넷에 대한 AndroidManifest를에 권한을 추가하려면 어떻게해야 다음에 질문을하기 전에 숙제를 먼저 해 googleing을해라. 희망이 당신을 도울 수 있습니다.

+0

ur 도움을 주셔서 감사합니다. – user1365148

-1

Activity의 onCreate()에서 StartActivity() 메소드를 시작하고 싶습니까? 당신은

HttpClient 를 사용하여 HTTP 요청에 가서

+0

그 URL에서 위도와 랭 위치를 원하십니까? – user1365148

+0

그래서 내가 sqllite 데이터베이스에 제출할 수 있습니까? – user1365148

+0

인 텐트 호출 및 시작 활동은 활동을 시작하고 인 텐트를 가진 Uri는 기본 브라우저를 호출하여 URI를 엽니 다. –

관련 문제