将字符串转换为经度和纬度
问题描述:
我想在我的应用程序内部实现谷歌地图,并试图将字符串转换为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));
}
}
}
}
答
试试这个:
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.
所以它总是会返回一个地址列表
答
若要将地址字符串转换为其他地址,请考虑使用地址验证服务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响应中读出你想要的数据。
所以基本上,您希望我们为您自己的目的编辑其他人的代码? – HavelTheGreat 2015-02-06 19:48:06
正如@Elizion暗示的那样,这不应该做太多的修改来做你想做的事情,看起来你没有真正尝试过在教程之外进行复制。 – zgc7009 2015-02-06 19:50:35
哦,我的天..我修改了它..我想从一个字符串中创建一个地址,而不是从列表 ...如果我删除这些注释,您感觉会更好......? – Mike 2015-02-06 19:56:56