将String转换为纬度和经度

时间:2015-02-06 19:45:04

标签: android geolocation maps

我想在我的应用程序中实现一个谷歌地图,我正在尝试将一个字符串转换为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 个答案:

答案 0 :(得分: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.

因此它将始终返回地址列表

答案 1 :(得分: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响应中读取您想要的数据。