2015-02-06 1 views
1

내 앱 내부에 Google지도를 구현하고 String을 위도와 경도로 변환하려고 시도하고 있습니다 (예 : "USA New York -> 2345.423423"). 저는 인터넷에서 검색을했고, 위도와 경도를 String으로 변환하는 많은 예제를 발견했습니다. 그러나 왜 내가 사는 곳의 위도와 경도를 알지 못하기 때문에 이유를 이해할 수 없습니다. .. 나는 무언가를 발견했으며 작동하지만 주소 목록으로 변환됩니다. 나는 가능한 한 쉽게 뭔가를 원한다. 당신을 감사하고 좋은 하루문자열을 위도와 경도로 변환

package com.currencymeeting.activities; 

import java.io.IOException; 
import java.util.List; 

import android.location.Address; 
import android.location.Geocoder; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.widget.Toast; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class GoogleMapsActivity extends FragmentActivity{ 

GoogleMap googleMap; 
MarkerOptions markerOptions; 
LatLng latLng; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_google_maps); 

    SupportMapFragment supportMapFragment = (SupportMapFragment) 
      getSupportFragmentManager().findFragmentById(R.id.googleMap); 

    googleMap = supportMapFragment.getMap(); 

    new GeocoderTask().execute("USA New York"); 

} 

private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{ 

    @Override 
    protected List<Address> doInBackground(String... locationName) { 
     // Creating an instance of Geocoder class 
     Geocoder geocoder = new Geocoder(getBaseContext()); 
     List<Address> addresses = null; 

     try { 
      // Getting a maximum of 3 Address that matches the input text 
      addresses = geocoder.getFromLocationName(locationName[0], 1); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return addresses; 
    } 

    @Override 
    protected void onPostExecute(List<Address> addresses) { 

     if(addresses==null || addresses.size()==0){ 
      Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show(); 
     } 

     // Clears all the existing markers on the map 
     googleMap.clear(); 

     // Adding Markers on Google Map for each matching address 
     for(int i=0;i<addresses.size();i++){ 

      Address address = (Address) addresses.get(i); 

      // Creating an instance of GeoPoint, to display in Google Map 
      latLng = new LatLng(address.getLatitude(), address.getLongitude()); 

      String addressText = String.format("%s, %s", 
      address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", 
      address.getCountryName()); 

      markerOptions = new MarkerOptions(); 
      markerOptions.position(latLng); 
      markerOptions.title(addressText); 

      googleMap.addMarker(markerOptions); 

      // Locate the first location 
      if(i==0) 
       googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,16)); 
     } 
    } 
} 

}

+2

기본적으로, 우리는 당신 자신의 목적을 위해 다른 사람의 코드를 편집하기를 원하십니까? – HavelTheGreat

+0

@ 엘리 손 (Elizion)이 암시하고있는 것처럼, 이것은 당신이 원하는 것을하기 위해 많은 수정을해서는 안되며, 튜토리얼을 복사하지 않고 많은 것을 시도한 것처럼 보입니다. – zgc7009

+0

오 세상에 .. 나는 그것을 수정했다 .. 목록에서 아닌 문자열에서 주소를 만들고 싶다.> ... 나는 그 주석을 지우면 더 잘 벌리고있다 ...? – Mike

답변

0

가지고 노력이 :

또한
Geocoder geocoder; 
List<Address> addresses; 
geocoder = new Geocoder(this, Locale.getDefault()); 
addresses = geocoder.getFromLocation(latitude, longitude, 1); 

String address = addresses.get(0).getAddressLine(0); 
String city = addresses.get(0).getAddressLine(1); 
String country = addresses.get(0).getAddressLine(2); 

할 수 있습니다 check this 후 밖으로 너무

편집

List<Address> getFromLocation (double latitude, double longitude, int maxResults) 
    takes following paramters: 
    latitude- the latitude a point for the search 
    longitude- the longitude a point for the search 
    maxResults- max number of addresses to return. 

항상 주소 목록을 반환합니다.

+0

감사합니다. 목록

의 사용을 피할 방법이 없음을 이해합니다. – Mike

+0

전체 목록에 하나의 주소 만 있으면 목록의 첫 번째 요소를 필요한 함수로 반환하고 그에 따라 처리 할 수 ​​있습니다. getLocationFrom() 함수의 세 번째 매개 변수는 함수가 최대 1 개의 결과를 반환해야하므로 결과는 주소 목록의 첫 번째 요소가됩니다. – sujay

0

주소 문자열을 다른 것으로 변환하려면 SmartyStreets에서 제공 한 주소 확인 서비스 API를 사용하는 것이 좋습니다.

예를 들어, 뉴욕시의 위도와 경도를 원합니다. 많은 정보를 반환

https://smartystreets.com/demo?city=new+york&state=ny

을하지만, 구체적으로는 뉴욕시에서 ZIP 코드의 모든 위도와 경도를 가지고 : 그래서 다음과 같이 자신의 데모로 도시와 국가를 공급. 우편 번호를 지정하면보다 구체적인 응답이 반환됩니다.

이 같은 요청에 직접 API를 칠 경우

curl -v 'https://api.smartystreets.com/zipcode?auth-id=[your-auth-id]& 
    auth-token=[your-auth-token]' 
    --data-binary '[{"city":"New York","state":"NY","zipcode":"10001"}]' 

당신은이 같은 JSON 응답을 얻을 것이다 :이 같은 서비스는 문자열 LAT을 할 수

[ 
    { 
     "input_index": 0, 
     "city_states": [ 
      { 
       "city": "New York", 
       "state_abbreviation": "NY", 
       "state": "New York", 
       "mailable_city": true 
      } 
     ], 
     "zipcodes": [ 
      { 
       "zipcode": "10001", 
       "zipcode_type": "S", 
       "county_fips": "36061", 
       "county_name": "New York", 
       "latitude": 40.74949, 
       "longitude": -73.98935 
      } 
     ] 
    } 
] 

를/당신을위한 lon 변환은 정말 쉽습니다. JSON 응답에서 원하는 데이터를 읽는 것만으로도 충분합니다.

관련 문제