如何使用字符串地址获取 google 地图 API V2 android 中的地理坐标?

How to get geographical coordinates in google maps API V2 android using an string address?

我正在设计一个 android 定位地点的应用程序,这些地点位于服务器的数据库中,但我只有地点的名称和位置,因此我需要在我的应用程序中找到它并在那里放一个 marker 。仅通过地址获取坐标是可能的,或者,我是否需要重做我的数据库,添加带有纬度和经度的字段?

您可以向 google 地图 API 发出请求,以通过地址字符串获取可能的地址。检查此 link

您可以使用 Geocoder class 查找您拥有的地址,然后使用返回的 LanLng 对象用标记填充地图。

请注意,Geocoder class 无法对每个地址进行地理编码,但如果格式正确,大多数地址都会成功。

this question 中的代码作为指导,我刚刚让这个简单的示例运行起来。

我创建了一个自定义 class 来存储位置名称、位置地址和一个 LatLng 对象来存储 lat/lon。

对于这个简单的例子,我只使用了三个地址。

这是完整的 class 代码:

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MapsActivity extends AppCompatActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }


    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }


    private void setUpMap() {
        mMap.setMyLocationEnabled(true);

        List<CustomLocation> custLocs = new ArrayList<CustomLocation>();

        //Testing with three addresses
        custLocs.add(new CustomLocation("location 1", "100 market street san francisco ca"));
        custLocs.add(new CustomLocation("location 2", "200 market street san francisco ca"));
        custLocs.add(new CustomLocation("location 3", "300 market street san francisco ca"));


        //set the location for each item in the list
        for (CustomLocation custLoc : custLocs){
            custLoc.setLocation(getSingleLocationFromAddress(custLoc.address));
        }

        //draw the Marker for each item in the list
        for (CustomLocation custLoc : custLocs){
            mMap.addMarker(new MarkerOptions().position(custLoc.latLng)
                    .title(custLoc.name).icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
        }
    }

    //method to do a lookup on the address
    public LatLng getSingleLocationFromAddress(String strAddress)
    {
        Geocoder coder = new Geocoder(this, Locale.getDefault());
        List<Address> address = null;
        Address location = null;
        LatLng temp = null;
        String strAddresNew = strAddress.replace(",", " ");
        try
        {
            address = coder.getFromLocationName(strAddresNew, 1);
            if (!address.isEmpty())
            {
                location = address.get(0);
                location.getLatitude();
                location.getLongitude();
                temp = new LatLng(location.getLatitude(), location.getLongitude());
                Log.d("Latlng : ", temp + "");
            }
        } catch (IOException e)
        {
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return temp;
    }

    //class to hold the name and address and location
    public static class CustomLocation{

        public String name;
        public String address;
        public LatLng latLng;
        public CustomLocation(String n, String a){
            name = n;
            address = a;
        }
        public void setLocation(LatLng ll){
            latLng = ll;
        }
    }
}

结果:

您可以使用地理编码器 class,它将 return 指定地址中给定地址的纬度和经度 format.However 地址名称必须遵循指定格式. 从下面查看更多信息 link :- https://developer.android.com/reference/android/location/Geocoder