我制作了一个 Android 应用程序,它在 Android 屏幕Google Map
的上半部分Android Screen
和下半部分显示一个 TextView。
在我的 上Google Maps(on the Top half of the android screen)
,我正在我当前的位置上画一个Circle
,我正在current location (lat and long values)
从DDMS perspective in the emulator
. 它在模拟器中运行良好。
当我在模拟器上启动应用程序时,首先它在 android 屏幕的上半部分显示谷歌地图,在下半部分显示一个 TextView。然后使用 DDMS Perspective,我传递了我当前的位置(纬度和经度值),通过之后,它在我在谷歌地图上的当前位置上绘制了一个圆圈。
在模拟器中一切正常。
问题陈述:-
但是当我在 real 上尝试同样的事情时Android Phone
,只有我Google Maps
的在 Android 屏幕的上半部分正确显示,而 TextView 在 Android 屏幕的下半部分正确显示。但它不是在绘制Circle on my Current Location
.
我的 Android 手机已连接到 Wi-Fi。而且我不认为我需要在 Android 手机上传递纬度和经度值,对吧?它应该自动获取当前位置对吗?
或者我需要在我的 Android 手机上做些什么?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
mapView = (MapView) findViewById(R.id.mapView);
listView = (ListView) findViewById(R.id.mylist);
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(15);
}
private class GPSLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
findUsersInCurrentRadius(4,location.getLatitude(),location.getLongitude());
mapController.animateTo(point);
mapController.setZoom(15);
// add marker
MapOverlay mapOverlay = new MapOverlay(this,android.R.drawable.star_on,R.drawable.tenm,R.drawable.twentym,R.drawable.thirtym,R.drawable.fourtym);
mapOverlay.setPointToDraw(point);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
String address = ConvertPointToLocation(point);
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
mapView.invalidate();
}
}
class MapOverlay extends Overlay {
private GeoPoint pointToDraw;
int[] imageNames=new int[6];
public MapOverlay(GPSLocationListener gpsLocationListener,
int currentUser, int tenm, int twentym, int thirtym, int fourtym) {
imageNames[0]=currentUser;
imageNames[1]=tenm;
imageNames[2]=twentym;
imageNames[3]=thirtym;
imageNames[4]=fourtym;
}
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(pointToDraw, screenPts);
//--------------draw circle----------------------
Point pt = mapView.getProjection().toPixels(pointToDraw,screenPts);
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(0x30000000);
circlePaint.setStyle(Style.FILL_AND_STROKE);
int totalCircle=4;
int radius=40;
int centerimagesize=35;
for (int i = 1; i <= totalCircle; i ++) {
canvas.drawCircle(screenPts.x,screenPts.y, i*radius, circlePaint);
}
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),imageNames[0]), (screenPts.x-(centerimagesize/2)),(screenPts.y-(centerimagesize/2)), null);
super.draw(canvas,mapView,shadow);
return true;
}
}
还有我的 AndroidManifest.xml 文件——
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.circlemapandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".ThesisProjectAndroid"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>