将字符串转换为经纬度

Convert String into latitude and longitude

我想在我的应用程序中实现 google 地图,我正在尝试将字符串转换为纬度和经度,例如 "USA New York -> 2345.423423"。我在网上搜索了一下,找到了很多将lat和long转换成String的例子,但是我不明白为什么,因为我连我住的地址的lat和long都不知道。 .. 我找到了一些东西并且它有效,但它会转换成地址列表。如果可能的话,我想要更简单的东西。谢谢你,祝你有美好的一天

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 post 出去也

编辑

  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.

所以它总是return一个地址列表

要将地址字符串转换成其他内容,请考虑使用地址验证服务 API,例如 SmartyStreets 提供的服务。

例如,您需要纽约市的纬度和经度。因此,将城市和州输入到他们的演示中,如下所示:

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

那 return 包含很多信息,但具体而言,它包含纽约市所有邮政编码的纬度和经度。如果您要指定一个 ZIP,那么它会 return 一个更具体的响应。

如果您直接用这样的请求点击他们的 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 响应中读取您想要的数据。