2012-04-17 26 views
0

我對googlemap應用程序有一點小問題,我想用這個tutorial來處理我的Eclipse,我已經更新了所有的Elipse ADT,下載了GoogleAPI,但是我仍然收到IDE我的HelloWorld應用程序將從中擴展的MapActivity無法解析爲類型。我是否錯過了一些重要的導入或設置,我需要在Eclipse上執行以使其正常工作?已添加GoogleAPI,但MapActivity變得不可見

public class HelloGoogleMapActivity extends MapActivity { 
    /** Called when the activity is first created. */ 
    @override 
    protected boolean isRoundDisplayed() 
    { 
     return false; 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     MapView mapv=(MapView)findViewById(R.id.mapview); 
     mapv.setBuildInZoomControls(true); 
    } 
} 

此外,第一個「@override」關鍵字被報告爲非法,我不知道爲什麼。

[UPDATE]

提供的所有答案都是正確的。最正確的是下面的評論。感謝您的代碼剪輯。涼!

+1

你改變你的項目目標的任何谷歌的API X,而不是任何Android X? – 2012-04-17 06:16:24

+0

在eclipse上chk你的sdk安裝是否正確? – Shruti 2012-04-17 06:25:49

回答

0

使用下面爲您所使用的代碼....
從代碼

public class GoogleMapsActivity extends MapActivity implements LocationListener{ 
/** Called when the activity is first created. */ 
MapView mapView; 
MapController mc; 
GeoPoint p; 
String add = ""; 
double lattitude ; 
double longitude; 
String selectedprofile; 
AudioManager am; 
Button search; 
EditText placeSearch; 


class MapOverlay extends com.google.android.maps.Overlay 
{ 
    @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(p, screenPts); 

     //---add the marker--- 
     Bitmap bmp = BitmapFactory.decodeResource(
      getResources(), R.drawable.pushpin2);    
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   
     return true; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    { 
     add=""; 
     //---when user lifts his finger--- 
     if (event.getAction() == 1) {     
      GeoPoint p = mapView.getProjection().fromPixels(
       (int) event.getX(), 
       (int) event.getY()); 


      Geocoder geoCoder = new Geocoder(
        getBaseContext(), Locale.getDefault()); 
       try { 
        List<Address> addresses = geoCoder.getFromLocation(
         p.getLatitudeE6()/1E6, 
         p.getLongitudeE6()/1E6, 1); 

        lattitude=p.getLatitudeE6()/1E6; 
        longitude=p.getLongitudeE6()/1E6; 

        if (addresses.size() > 0) 
        { 
         for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
          i++) 
          add += addresses.get(0).getAddressLine(i) + "\n"; 
        } 

        Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show(); 


       } 
       catch (IOException e) {     
        e.printStackTrace(); 
       } 
       return true; 

     }     
     else { 
     return false; 
     } 
    }   
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Intent intent=getIntent(); 
    setResult(RESULT_OK, intent); 



    mapView = (MapView) findViewById(R.id.mapView); 
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
     new LinearLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true); 

    mc = mapView.getController(); 
    String coordinates[] = {"1.352566007", "103.78921587"}; 
    double lat = Double.parseDouble(coordinates[0]); 
    double lng = Double.parseDouble(coordinates[1]); 

    p = new GeoPoint(
     (int) (lat * 1E6), 
     (int) (lng * 1E6)); 

    mc.setCenter(p); 
    mc.setZoom(17); 

    //---Add a location marker--- 
    MapOverlay mapOverlay = new MapOverlay(); 
    List<Overlay> listOfOverlays = mapView.getOverlays(); 
    listOfOverlays.clear(); 
    listOfOverlays.add(mapOverlay);  

    //mapView.invalidate(); 
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this); 
} 
1

爲@覆蓋例外JST變化需要一次JavaCompiler進行此代碼的工作正常,當我嘗試顯示地圖...改變你項目屬性... JDK合規1.6到1.5,並再次將其設置爲1.6 ...我面臨着同樣的概率..它是一些jdk構建項目的bcz .​​..或日食概率...

for map Prob

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <com.google.android.maps.MapView 
     android:id="@+id/showmap" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:apiKey="@string/mapkey" 
     android:clickable="true" /> 

</RelativeLayout> 

Activity.java

MapView mapView; 

@Override 
    protected void onCreate(Bundle icicle) { 

     try { 
      super.onCreate(icicle); 
      setContentView(R.layout.map_screen); 
      mapView = (MapView) findViewById(R.id.showmap); 
      mapView.setBuiltInZoomControls(true); 
      final MapController mc = mapView.getController(); 
      mc.animateTo(new GeoPoint(((int) (latitude * 1E6) - 10), 
        ((int) (longitude * 1E6)) - 10)); 
      mc.setZoom(5); 
     } catch (Exception e) { 
      Log.e("MapCreate", e.getMessage()); 
     } 

ApplicationManifest.xml

<uses-permission android:name="android.permission.INTERNET" /> 
     <application 
       android:icon="@drawable/ic_launcher" 
       android:label="Andro Integration" 
       android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > 


       <uses-library android:name="com.google.android.maps" /> 
    <!--   <activity --> 

<!--   </activity> --> 

     </application> 
1

的java

package com.manit.HelloGoogleMaps2; 

import java.util.List;   
import android.graphics.drawable.Drawable;  
import android.os.Bundle; 
import com.google.android.maps.GeoPoint;  
import com.google.android.maps.MapActivity;  
import com.google.android.maps.MapView;  
import com.google.android.maps.Overlay;  
import com.google.android.maps.OverlayItem;  

public class HelloGoogleMaps2 extends MapActivity {  
/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  

    MapView mapView = (MapView) findViewById(R.id.mapview);  
    mapView.setBuiltInZoomControls(true); 

    List<Overlay> mapOverlays = mapView.getOverlays(); 
    Drawable drawable = this.getResources().getDrawable(R.drawable.icon); 
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this); 
    GeoPoint point = new GeoPoint(30443769,-91158458); 
    OverlayItem overlayitem = new OverlayItem(point, "hi", "I'm in India!"); 

    GeoPoint point2 = new GeoPoint(17385812,78480667); 
    OverlayItem overlayitem2 = new OverlayItem(point2, "hi!", "I'm in Ahmedabad, India!"); 

    itemizedoverlay.addOverlay(overlayitem); 
    itemizedoverlay.addOverlay(overlayitem2); 

    mapOverlays.add(itemizedoverlay); 
} 

@Override 
protected boolean isRouteDisplayed() 
{ 
return false; 
} }  

HelloItemizedOverlay.java

package com.manit.HelloGoogleMaps2; 

import java.util.ArrayList; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.OverlayItem; 

public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem>{ 
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
private Context mContext; 

public HelloItemizedOverlay(Drawable defaultMarker, Context context) { 
    // TODO Auto-generated constructor stub 
    super(boundCenterBottom(defaultMarker)); 
    mContext = context; 
} 

public void addOverlay(OverlayItem overlay) 
{ 
mOverlays.add(overlay); 
populate(); 
} 
@Override 
protected OverlayItem createItem(int i) 
{ 
return mOverlays.get(i); 
} 
@Override 
public int size() 
{ 
return mOverlays.size(); 
} 
@Override 
protected boolean onTap(int index) 
{ 
OverlayItem item = mOverlays.get(index); 
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
dialog.setTitle(item.getTitle()); 
dialog.setMessage(item.getSnippet()); 
dialog.show(); 
return true; 
} 

} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
      <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
        package="com.manit.HelloGoogleMaps2" 
        android:versionCode="1" 
        android:versionName="1.0"> 
       <uses-sdk android:minSdkVersion="7" /> 

       <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <activity android:name=".HelloGoogleMaps2" 
           android:label="@string/app_name"> 
         <intent-filter> 
          <action android:name="android.intent.action.MAIN" /> 
          <category android:name="android.intent.category.LAUNCHER" /> 
         </intent-filter> 
        </activity> 
      <uses-library android:name="com.google.android.maps" /> 
       </application> 
       <uses-permission android:name="android.permission.INTERNET" /> 
      </manifest> 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <com.google.android.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mapview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:clickable="true" 
    android:apiKey="0Y2GRNdvsKsNO5cbkNKYcht3_0ASApwak-Q19Fg" 
    /> 

string.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="hello">Hello World, HelloGoogleMaps!</string> 
<string name="app_name">Hello,GoogleMaps</string> 
<string name="mapskey">0Y2GRNdvsKsNO5cbkNKYcht3_0ASApwak-Q19Fg</string> 
</resources> 
+0

這是Android地圖示例的完整代碼。我希望這有助於。 – 2012-04-17 06:49:51