如何使用字符串地址,以获得在谷歌地图API V2 Android的地理坐标? [英] How to get geographical coordinates in google maps API V2 android using an string address?

查看:341
本文介绍了如何使用字符串地址,以获得在谷歌地图API V2 Android的地理坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm设计一款Android应用,找到的地方,这些地方都是在服务器的数据库,但我只有名称和地点的位置,所以我需要找到它在我的应用程序,并把一个的标记的存在。这是更多钞票来获得坐标只与地址,或者,我需要重做我的数据库添加经度和纬度领域?

I´m designing an android app that locate places, these places are in a database in a server but I only have the name and location of the place, so I need to locate it in my app and put a marker there. It's posible to get coordinates only with address or, Do I need to redo my database adding fields with latitude and longitude?

推荐答案

您可以使用的地理codeR 类做你有地址的查询,然后填充使用标记的地图 LanLng 返回的对象

You can use the Geocoder class to do a look-up of the addresses you have, and then populate the map with Markers using the LanLng objects that are returned.

请注意,地理codeR 类将无法地理code每个地址,但它一定会成功了一大半,如果他们正确的格式。

Note that the Geocoder class will not be able to geocode every address, but it will be successful for most of them if they are in the correct format.

从<一个以code href=\"http://stackoverflow.com/questions/30521933/how-to-get-latitude-longitude-from-more-than-500-addresses\">this问题为指导,我刚刚得到这个简单的例子工作。

Taking code from this question as a guide, I just got this simple example working.

我创建的存储位置名称,位置的地址,以及经纬度对象来存储自定义类的纬度/经度。

I created a custom class that stores location name, location address, and a LatLng object to store the lat/lon.

有关这个简单的例子,我只是用三个地址。

For this simple example, I just used three addresses.

下面是完整的类code:

Here is the full class code:

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;
        }
    }
}

结果:

这篇关于如何使用字符串地址,以获得在谷歌地图API V2 Android的地理坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆