我正在开发一款带谷歌地图的Android应用。首先,我使用一些谷歌地图代码制作了一个单独的项目。这个程序工作正常。接下来,我在主要活动中创建了一个具有两个活动的新应用程序,我有一个按钮,当我按下它时,第二个活动开始。在第二个活动中,我将第一个项目的代码进行了微小的更改,以获得正确的数据包名称等。
当我运行应用程序并按下按钮时,第二个活动开始,但我得到一个带有缩放按钮的空白地图。我查看了日志,我得到了错误,现在与te google服务器连接。我检查了我放入清单的api密钥,但这是oke。甚至生成了一个具有相同结果的新的。我做错了什么或遗忘了什么?
下面是清单文件和第二个活动代码。
<?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
}
}
答案 0 :(得分:0)
您是否仔细检查过您正在运行此设备的设备上是否安装了Google地图应用?您的问题与此问题非常相似:
Google Maps Android API V2 check if GoogleMaps are installed on device
答案 1 :(得分:0)
这是一个非常愚蠢的拼写错误。拼写定位器错误。现在代码工作得很好。谢谢你的帮助。
我正在使用Genymotion模拟器btw。 eclipse捆绑包附带的模拟器不支持OpenGL ES V2。只是想提一下,因为我在开始时就遇到过这个问题并且已经读过很多人已经或者遇到过这个问题。
再次感谢您的帮助。
答案 2 :(得分:0)
通过Android SDK Manager更新您的Google Play服务,然后通过在eclipse中导入它来使用它,因为它有新的更新,然后将这些行添加到元素中的清单文件中,这些文件在您的元素中缺失 -
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
从设备中卸载应用程序并安装新版本。