Google地图正在显示但未在Android中加载地图和路线

时间:2014-10-29 08:09:33

标签: android google-maps

我想在我的项目中使用Google地图。它在一个项目中正常工作,但是当我在另一个项目中使用相同的代码时,地图上没有显示。以下是我的代码部分:

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], 3);
        } 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.newLatLng(latLng));
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您必须在Google控制台上制作新项目,并且必须为新项目创建另一个API密钥。然后在新项目中使用此API密钥。 它对我有用。

答案 1 :(得分:0)

您必须为项目创建新的API密钥。 以下是生成密钥的步骤。

https://developers.google.com/maps/documentation/android/start

希望这会对你有所帮助。

答案 2 :(得分:0)

您需要获取包含应用包名称的API密钥,这样才能正确加载地图。

相关问题