我正在开发带有 Google 地图的 Android 应用程序。首先,我用一些谷歌地图代码制作了一个单独的项目。这个应用程序运行良好。接下来我制作了一个新应用程序,主活动中有两个活动,我有一个按钮,当我按下它时,第二个活动开始。在第二个活动中,我将第一个项目的代码稍作更改,以获得正确的数据包名称等。
当我运行应用程序并按下按钮时,第二个活动开始,但我得到一张带有缩放按钮的空白地图。我查看了日志,我得到了现在与 te google 服务器建立连接的错误。我检查了我放在清单中的 api 密钥,但这没关系。甚至生成了一个具有相同结果的新文件。我做错了什么或忘记了什么?
下面是清单文件和第二个活动代码。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.datoeter.locater"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.datoeter.locator.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
// Permissions that are needed
<uses-permission android:name="com.datoeter.locator.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.datoeter.locater.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.datoeter.locater.TrackActivity"
android:label="@string/title_activity_track" >
</activity>
// API information
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Omitted API key" />
</application>
</manifest>
和活动代码
package com.datoeter.locater;
import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
public class TrackActivity extends FragmentActivity implements LocationListener {
//Google map object
GoogleMap Gmap;
boolean FirstTime = true;
//Location variables
LatLng OldLoc;
LatLng NewLoc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_track);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}
else
{
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment supportmapfragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
Gmap = supportmapfragment.getMap();
// Enabling MyLocation Layer of Google Map
Gmap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 10000, 0, this);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
if(FirstTime){
// Get starting position
OldLoc = latLng;
// Add start marker
Marker Start = Gmap.addMarker(new MarkerOptions()
.position(OldLoc)
.title("Start")
.snippet("You started here")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
FirstTime = false;
}
else{
// Get new position
NewLoc = latLng;
// Draw route (line) from last location to new location on map
Polyline Route = Gmap.addPolyline(new PolylineOptions()
.add(OldLoc,
NewLoc)
.geodesic(false));
// New position will be old position for next change
OldLoc = NewLoc;
}
// Showing the current location in Google Map
Gmap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
Gmap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}