我正在开发一个涉及谷歌地图的应用程序。
我尝试使用新的 V2 谷歌地图,但事实证明只有基于 4.2.2 的设备可以运行它。
如何为我的应用程序制作所有 android 版本可完成的地图?
非常感谢。
它不适用于我的 HTC DESIRE HD (4.2.2)
编码:
package com.example.free;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
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;
public class MainActivity extends Activity {
public void ToastLoadShout(String msg){Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
static final LatLng gps = new LatLng(0, 0);
static double lat=0.0;
static double lon=0.0;
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
turnGPSOn();
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
// .title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Free shyt")
.snippet("Come and take this shit")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
ImageView locate;
locate = (ImageView) findViewById(R.id.locate);
locate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ReNewCoordinates();
if (lat==0&&lon==0)
ToastLoadShout("Try again in a few seconds.");
else
{
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lon), 10));
ToastLoadShout("your location is: "+lat+" , "+lon);
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
Marker loc = map.addMarker(new MarkerOptions()
.position(new LatLng(lat,lon))
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.loc)));
}
}
});
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(50.909474,13.917618), 10000));
map.animateCamera(CameraUpdateFactory.zoomTo(1), 20, null);
// Move the camera instantly to hamburg with a zoom of 15.
//map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lon), 15));
// Zoom in, animating the camera.
//map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
//ToastLoadShout("your location is: "+lat+" , "+lon);
ImageView add;
add = (ImageView) findViewById(R.id.add);
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Add.class);
startActivity(intent);
}
});
}
private void turnGPSOn() {
String provider = android.provider.Settings.Secure.getString(
getContentResolver(),
android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) { // if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
ToastLoadShout("Turning GPS on..");
}
}
/*private void getLocation() {
// Get the location manager
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
LocationListener loc_listener = new LocationListener() {
public void onLocationChanged(Location l) {}
public void onProviderEnabled(String p) {}
public void onProviderDisabled(String p) {}
public void onStatusChanged(String p, int status, Bundle extras) {}
};
locationManager
.requestLocationUpdates(bestProvider, 0, 0, loc_listener);
location = locationManager.getLastKnownLocation(bestProvider);
try {
lat = location.getLatitude();
lon = location.getLongitude();
} catch (NullPointerException e) {
lat = -1.0;
lon = -1.0;
}
}
*/
public void ReNewCoordinates(){
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
lat = (location.getLatitude());
lon = (location.getLongitude());
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,Bundle extras) {}};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
@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;
}
}
使用 HTC 的 logcat:
08-01 21:47:39.645: I/Process(5270): Sending signal. PID: 5270 SIG: 9
08-01 21:47:43.980: D/skia(5310): new locale en-Latn-GB
08-01 21:47:44.120: W/GooglePlayServicesUtil(5310): Google Play services out of date. Requires 3159100 but found 2012110
08-01 21:47:44.130: W/GooglePlayServicesUtil(5310): Google Play services out of date. Requires 3159100 but found 2012110
08-01 21:47:44.130: W/GooglePlayServicesUtil(5310): Google Play services out of date. Requires 3159100 but found 2012110
08-01 21:47:44.140: W/GooglePlayServicesUtil(5310): Google Play services out of date. Requires 3159100 but found 2012110
08-01 21:47:44.150: W/GooglePlayServicesUtil(5310): Google Play services out of date. Requires 3159100 but found 2012110
08-01 21:47:44.210: W/GooglePlayServicesUtil(5310): Google Play services out of date. Requires 3159100 but found 2012110
08-01 21:47:44.210: D/AndroidRuntime(5310): Shutting down VM
08-01 21:47:44.210: W/dalvikvm(5310): threadid=1: thread exiting with uncaught exception (group=0x40b10930)
08-01 21:47:44.220: E/AndroidRuntime(5310): FATAL EXCEPTION: main
08-01 21:47:44.220: E/AndroidRuntime(5310): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.MainActivity}: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.app.ActivityThread.access$600(ActivityThread.java:153)
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.os.Looper.loop(Looper.java:137)
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.app.ActivityThread.main(ActivityThread.java:5227)
08-01 21:47:44.220: E/AndroidRuntime(5310): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 21:47:44.220: E/AndroidRuntime(5310): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 21:47:44.220: E/AndroidRuntime(5310): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-01 21:47:44.220: E/AndroidRuntime(5310): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-01 21:47:44.220: E/AndroidRuntime(5310): at dalvik.system.NativeStart.main(Native Method)
08-01 21:47:44.220: E/AndroidRuntime(5310): Caused by: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
08-01 21:47:44.220: E/AndroidRuntime(5310): at com.google.android.gms.internal.x.b(Unknown Source)
08-01 21:47:44.220: E/AndroidRuntime(5310): at com.google.android.gms.maps.model.BitmapDescriptorFactory.aX(Unknown Source)
08-01 21:47:44.220: E/AndroidRuntime(5310): at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromResource(Unknown Source)
08-01 21:47:44.220: E/AndroidRuntime(5310): at com.example.free.MainActivity.onCreate(MainActivity.java:53)
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.app.Activity.performCreate(Activity.java:5104)
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-01 21:47:44.220: E/AndroidRuntime(5310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
08-01 21:47:44.220: E/AndroidRuntime(5310): ... 11 more
第 53 行:
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("title")
.snippet("description")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));