我想在我的应用程序中实现一个谷歌地图,我正在尝试将字符串转换为lat和long,类似于“USA New York”-
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));
}
}
}
}
试试这个:
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);
你也可以看看这个帖子
编辑
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.
因此它将始终返回地址列表
要将地址字符串转换为其他字符串,请考虑使用地址验证服务API,例如SmartyStreets提供的API。
例如,您需要纽约市的纬度和经度。因此,将城市和州输入到他们的演示中,如下所示:
https://smartystreets.com/demo?city=new约克
这将返回大量信息,但特别是它具有纽约市所有邮政编码的纬度和经度。如果您要指定邮政编码,那么它将返回更具体的响应。
如果您直接使用这样的请求命中他们的 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
}
]
}
]
像这样的服务可以非常容易地为您完成字符串到纬度/经度的转换。您只需要从JSON响应中读取您想要的数据。