我的Android Studio应用使用谷歌地图时遇到问题,问题是我的地图没有完全加载。我确实得到了满载的世界地图,但就目前而言。
应用程序本身的功能非常好,我可以输入我想要的任何国家/城市/地址,并在该位置添加一个标记(在我的简单头脑中告诉我,我拥有所有权限我需要利用我想要的功能来完成这个应用程序。
我一直在寻找解决方案几个小时,但我发现的一切,我已经启用和/或工作。我使用三星Galaxy S7手机来运行我的应用程序,到目前为止使用Android Studio没有任何问题。
我想这只是一种化妆品"问题,但我还想解决。
图片更好地描述了发生的事情:
Starting the app with custom markers.
Final zoom, with markers in the correct spot.
我的代码:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
EditText searchText;
public GoogleMap mMap;
private static final float DEFAULT_ZOOM = 15.0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
try {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
} catch (Exception e) {
e.printStackTrace();
}
searchText = (EditText) findViewById(R.id.input_search);
init();
}
@Override
public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show();
}
//init the search
public void init()
{
searchText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE)
{
geoLocate();
}
return false;
}
});
}
private void geoLocate()
{
String searchString = searchText.getText().toString();
Geocoder geocoder = new Geocoder(MapsActivity.this);
List<Address> list = new ArrayList<>();
try
{
list = geocoder.getFromLocationName(searchString, 1);
}
catch(Exception e)
{
e.printStackTrace();
}
if(list.size() > 0)
{
Address address = list.get(0);
LatLng position = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(position).title(searchText.getText().toString()));
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position, DEFAULT_ZOOM));
}
}
}
我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!--android:value="@string/google_maps_key" />-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="*Api*" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我的google_maps_api.xml文件:
<resources>
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">*Api*</string>
</resources>
我显然用 Api 排除了API_KEY。
这是我关于stackoverflow的第一篇文章,所以欢迎任何关于我的帖子质量的反馈。
答案 0 :(得分:1)
您是否可以确认您已在google开发者控制台中设置Android Google Maps SDK API密钥,如果没有,请在google开发者控制台中使用您的应用程序标识符和SHA1指纹启用Google Maps Android API?在为您的应用程序的特定调试/生产版本授权密钥之前,地图不会显示。