Google Maps API v2无法找到地址位置

时间:2015-12-15 07:49:52

标签: android google-maps google-maps-api-2 google-geocoder

我使用Google Maps Android API的GeoCoder类从我提供的地址字符串中检索特定属性的坐标。但Android应用程序无法找到该物业的Lat-Long。现在,当我将完全相同的字符串传递给https://www.google.com/maps/时,它可以轻松找到该房产的屋顶位置。它也适用于移动浏览器。

那是怎么回事?为什么Google地图无法在我的应用中找到该媒体资源?以下是我的代码:

    LatLng propertyLocation = null;
    try {
        propertyLocation = getLocationFromAddress("Property Address");
    } catch (IOException e) {
        e.printStackTrace();
    }

    MarkerOptions mOptions = new MarkerOptions();
    mOptions.position(propertyLocation).title("Property Location").snippet(propertyAddress)
            .draggable(true);

    propertyMarker = googleMap.addMarker(mOptions);
    propertyMarker.showInfoWindow();

    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(propertyLocation, 15), 3000,
            null);
    googleMap.setMyLocationEnabled(true);

这是我的getLocationFromAddress();方法:

private LatLng getLocationFromAddress(String subjectPropertyAddress) throws IOException {
    Log.i("Subject Property", subjectPropertyAddress);
    if (Geocoder.isPresent()) {
        Geocoder addressCoder = new Geocoder(LocationActivity.this);
        List<Address> addresses = addressCoder.getFromLocationName(subjectPropertyAddress, 2);
        if (addresses == null || addresses.isEmpty()) {
            Log.i("Addresses", "Could not locate address:");
            return null;
        } else {
            Address location = addresses.get(0);
            return new LatLng(location.getLatitude(), location.getLongitude());
        }
    } else {
        Log.i("Geo-Coder", "Not Present");
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过Asyntask课程?也许,这段代码可以帮到你

private class GeocodeTask extends AsyncTask<String, Void, List<Address>> {
    @Override
    protected List<Address> doInBackground(String... locationName) {
        Geocoder geocoder = new Geocoder(ctx);
        List<Address> addresses = null;
        try {
            // Getting a maximum of 3 Address that matches the input text
            addresses = geocoder.getFromLocationName(locationName[0], 3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addresses;
    }

    @Override
    protected void onPostExecute(List<Address> addresses) {
        if(addresses==null || addresses.size()==0){
            Toast.makeText(getActivity(), "No Location found", Toast.LENGTH_SHORT).show();
        }else {
            googleMap.clear();

            for(int i=0 ; i < addresses.size(); i++){
                Address address = (Address) addresses.get(i);
                latLng = new LatLng(address.getLatitude(), address.getLongitude());
                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13f));
                googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));               
            }
        }
    }
}

搜索位置:

new GeocodeTask().execute("Property Address");