2015-02-06 53 views
1

我想在我的應用程序內部實現谷歌地圖,並試圖將字符串轉換爲lat和long,例如「USA New York - > 2345.423423」。我在互聯網上搜索過,我發現了很多例子來將lat和long轉換爲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 2015-02-06 19:48:06

+0

正如@Elizion暗示的那樣,這不應該做太多的修改來做你想做的事情,看起來你沒有真正嘗試過在教程之外進行復制。 – zgc7009 2015-02-06 19:50:35

+0

哦,我的天..我修改了它..我想從一個字符串中創建一個地址,而不是從列表<> ...如果我刪除這些註釋,您感覺會更好......? – Mike 2015-02-06 19:56:56

回答

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 2015-02-06 20:06:09

+0

如果您確定只有一個地址會出現在整個列表中,您可以將列表的第一個元素返回到您需要的函數並相應地處理它。getLocationFrom()函數中的第三個參數表示該函數應返回最多1個結果,因此該結果將是您的地址列表的第一個元素 – sujay 2015-02-06 20:11:48

0

若要將地址字符串轉換爲其他地址,請考慮使用地址驗證服務API,例如由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響應:

[ 
    { 
     "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 
      } 
     ] 
    } 
] 

像這樣的服務可以做字符串LAT/lon轉換爲你真的很容易。你只需要從JSON響應中讀出你想要的數據。