我正在尝试制作Android谷歌地图,地图没有显示,并且出现“无法确定位置”的吐司,这意味着未检测到位置(= null)所以我有2个问题:
1-地图未显示
2-未检测到位置
我有2个Java文件
MyMapLocationActivity.java
package com.joshclemm.android.tutorial;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
public class MyMapLocationActivity extends MapActivity {
private MapView mapView;
private MyLocationOverlay myLocationOverlay;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// main.xml contains a MapView
setContentView(R.layout.main);
// extract MapView from layout
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// create an overlay that shows our current location
myLocationOverlay = new FixedMyLocationOverlay(this, mapView);
// add this overlay to the MapView and refresh it
mapView.getOverlays().add(myLocationOverlay);
mapView.postInvalidate();
// call convenience method that zooms map on our location
zoomToMyLocation();
}
@Override
protected void onResume() {
super.onResume();
// when our activity resumes, we want to register for location updates
myLocationOverlay.enableMyLocation();
}
@Override
protected void onPause() {
super.onPause();
// when our activity pauses, we want to remove listening for location updates
myLocationOverlay.disableMyLocation();
}
/**
* This method zooms to the user's location with a zoom level of 10.
*/
private void zoomToMyLocation() {
GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();
//System.out.println("myLocationGeoPoint is " +myLocationGeoPoint);
if(myLocationGeoPoint != null) {
System.out.println("Gowa !null");
mapView.getController().animateTo(myLocationGeoPoint);
mapView.getController().setZoom(10);
}
else {
Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show();
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
FixedMyLocationOverlay.java
package com.joshclemm.android.tutorial;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.location.Location;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Projection;
/**
* Fixes bug with some phone's location overlay class (ie Droid X).
* Essentially, it attempts to use the default MyLocationOverlay class,
* but if it fails, we override the drawMyLocation method to provide
* an icon and accuracy circle to mimic showing user's location. Right
* now the icon is a static image. If you want to have it animate, modify
* the drawMyLocation method.
*/
public class FixedMyLocationOverlay extends MyLocationOverlay {
private boolean bugged = false;
private Drawable drawable;
private Paint accuracyPaint;
private Point center;
private Point left;
private int width;
private int height;
public FixedMyLocationOverlay(Context context, MapView mapView) {
super(context, mapView);
}
@Override
protected void drawMyLocation(Canvas canvas, MapView mapView,
Location lastFix, GeoPoint myLocation, long when) {
if(!bugged) {
try {
super.drawMyLocation(canvas, mapView, lastFix, myLocation, when);
} catch (Exception e) {
// we found a buggy phone, draw the location icons ourselves
bugged = true;
}
}
if(bugged) {
if(drawable == null) {
accuracyPaint = new Paint();
accuracyPaint.setAntiAlias(true);
accuracyPaint.setStrokeWidth(2.0f);
drawable = mapView.getContext().getResources().getDrawable(R.drawable.ic_maps_indicator_current_position);
width = drawable.getIntrinsicWidth();
height = drawable.getIntrinsicHeight();
center = new Point();
left = new Point();
}
Projection projection = mapView.getProjection();
double latitude = lastFix.getLatitude();
double longitude = lastFix.getLongitude();
float accuracy = lastFix.getAccuracy();
float[] result = new float[1];
Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result);
float longitudeLineDistance = result[0];
GeoPoint leftGeo = new GeoPoint((int)(latitude*1e6), (int)((longitude-accuracy/longitudeLineDistance)*1e6));
projection.toPixels(leftGeo, left);
projection.toPixels(myLocation, center);
int radius = center.x - left.x;
accuracyPaint.setColor(0xff6666ff);
accuracyPaint.setStyle(Style.STROKE);
canvas.drawCircle(center.x, center.y, radius, accuracyPaint);
accuracyPaint.setColor(0x186666ff);
accuracyPaint.setStyle(Style.FILL);
canvas.drawCircle(center.x, center.y, radius, accuracyPaint);
drawable.setBounds(center.x - width/2, center.y - height/2, center.x + width/2, center.y + height/2);
drawable.draw(canvas);
}
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joshclemm.android.tutorial"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyMapLocationActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Make sure the uses-library line is inside the application tag -->
<uses-library android:name="com.google.android.maps" />
</application>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
布局的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:clickable="true"
android:apiKey="AIzaSyCx7eq9mnY609bDebfAEeBLx8L2Exw8J30" />
</LinearLayout>
这是完全出现的图像
答案 0 :(得分:0)
我看到你的MyMapLocationActivity.java是2010年的一个示例片段,可能是为Google Maps Android v1编写的。在v2中,事情的表现略有不同。
此代码段应该可以帮助您前进。请注意,在尝试缩放到当前位置之前,请使用OnGlobalLayoutListener
检查布局是否已完成。
public class ShowMapTest extends FragmentActivity {
private GoogleMap mMap;
private LinearLayout layout;
private LocationManager lm = null;
private LatLng mySpot = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearbymapview);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
Location loc = lm.getLastKnownLocation(lm.getBestProvider(criteria, false));
if (loc != null) {
mySpot = new LatLng(loc.getLatitude(), loc.getLongitude());
} else {
criteria.setAccuracy(Criteria.ACCURACY_FINE);
loc = lm.getLastKnownLocation(lm.getBestProvider(criteria, false));
if (loc == null) {
mySpot = new LatLng(39.952451,-75.163664); // city hall by default
}
}
// check if already instantiated
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMyLocationEnabled(true);
layout = (LinearLayout)findViewById(R.id.LinearLayout01);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// Center & zoom the map after map layout completes
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mySpot, 15));
}
});
} else {
mMap.clear();
}
// check if got map
if (mMap == null) {
Log.e("Couldn't get map fragment!", "No map fragment");
return;
}
nearbymapview.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_above="@+id/mapview" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:orientation="vertical">
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="4sp" android:paddingTop="4sp"
android:paddingRight="6sp">
<TextView android:id="@+id/TextViewT1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_marginLeft="2sp"
android:textSize="20sp" android:textStyle="bold" android:text="Test Map"></TextView>
<TextView android:id="@+id/TextViewT3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:textSize="20sp"
android:textStyle="bold"></TextView>
</RelativeLayout>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
清单中的:
<activity
android:name="ShowMapTest"
android:finishOnTaskLaunch="true"
android:launchMode="singleTop" >
</activity>
...
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR_KEY_HERE"/>