這是錯誤:在Google Maps中使用多標記Android
沒有可以訪問MainActivity類型的封閉實例。必須用封閉的MainActivity類型實例限定分配(例如,x.new A(),其中x是MainActivity的一個實例)。
錯誤在這行代碼中MyLocationListener.java在這行代碼
MyMapOverlays marker = new MyMapOverlays(p) ;
任何人出來幫我請?
繼承人的代碼:
MainActivity.class
package com.sample.dorm_locator_gps;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.os.Bundle;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class MainActivity extends MapActivity {
LocationManager locMgr;
MyLocationListener locLstnr;
private MapView mapView;
private MapController mc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();
String coordinates[] = {"30", "71"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(7);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/*My overlay Class starts*/
class MyMapOverlays extends com.google.android.maps.Overlay
{
GeoPoint location = null;
public MyMapOverlays(GeoPoint location)
{
super();
this.location = location;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
super.draw(canvas, mapView, shadow);
//translate the screen pixels
Point screenPoint = new Point();
mapView.getProjection().toPixels(this.location, screenPoint);
//add the image
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.our_cross_image),
screenPoint.x, screenPoint.y , null); //Setting the image location on the screen (x,y).
}
}
/*My overlay Class ends*/
}
MyLocationListener.java
package com.sample.dorm_locator_gps;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.sample.dorm_locator_gps.MainActivity.MyMapOverlays;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;
public class MyLocationListener implements LocationListener
{
private MapController mc;
private MapView mapView;
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitud = " +
loc.getLatitude() + "Longitud = " + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
String coordinates[] = {""+loc.getLatitude(), ""+loc.getLongitude()};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(7);
//add a location marker.
MyMapOverlays marker = new MyMapOverlays(p) ;
List listOfOverLays = mapView.getOverlays();
listOfOverLays.clear();
listOfOverLays.add(marker);
mapView.invalidate();
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
我能做些什麼? :( – user1519463
創建一個新類(在Eclipse中,它的文件 - >新建 - > Java的>類)。使用MyMapOverlays作爲類名,然後從MainActivity您MyMapOverlays類的代碼進入你剛纔提出的新類。希望這可以幫助。 – acj