2014-10-07 30 views
0

我想在android中製作地圖,這是爲了我的Skripsi,我在這裏下載源代碼https://github.com/ajaswal/GoogleMapsV2我打開了項目,但有問題,這是我的問題,我有關於無法投射的警報片段MapFragment,這個問題在27行無法將表單片段轉換爲Mapfragment

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

所以請幫我...

package com.example.googlemaps; 

import android.app.Activity; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 

import com.google.android.gms.maps.CameraUpdate; 
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.LatLng; 

public class MainActivity extends Activity { 
public final LatLng LOCATION_BURNABY= new LatLng(49.27645, -122.917587); 
public final LatLng LOCATION_SURREY= new LatLng(49.187500, -122.849000); 

private GoogleMap map; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
    if (map != null) { 
     setUpMap(); 
    } 
} 

private void setUpMap() { 
    // Enable MyLocation layer of Google map 
      map.setMyLocationEnabled(true); 

      // Get Location manager object from System service LOCATION_SERVICE 
      LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

      // Create a criteria object to retrieve provider 
      Criteria criteria = new Criteria(); 

      // Get the name of the best provider 

      String provider = locManager.getBestProvider(criteria, true); 

      // Get current location 
      Location myLocation = locManager.getLastKnownLocation(provider); 

      // Set map type 
      map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

      // Get the latitude of the current location 
      double latitude = myLocation.getLatitude(); 

      // Get the longitude of the current location 
      double longitude = myLocation.getLongitude(); 

      // Create a LatLng object for the current location 
      LatLng LOCATION_CURRENT = new LatLng(latitude, longitude); 

      // Show the current location in Google map 
      map.moveCamera(CameraUpdateFactory.newLatLng(LOCATION_CURRENT)); 

      // Zoom in the google map 
      CameraUpdate camUpdate = CameraUpdateFactory.newLatLngZoom(
        LOCATION_CURRENT, 15); 
      map.animateCamera(camUpdate); 

} 

@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; 
} 

public void onClick_Burnaby(View v){ 
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_BURNABY, 13); 
    map.animateCamera(update); 
} 

public void onClick_Surrey(View v){ 
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_SURREY, 14); 
    map.animateCamera(update); 
}} 
+3

顯示您的XMl文件。也用'FragmentActivity'擴展你的類。 – Piyush 2014-10-07 05:53:37

+0

http://www.learn2crack.com/2013/12/android-google-maps-api-v2-example.html – 2014-10-07 05:58:57

+0

發佈您的XML並且在您的項目中添加了'google-play-service_lib'作爲庫 – kId 2014-10-07 06:13:10

回答

0

使用FragmentActivity,而不是活動

示例 -

public class MapScreen extends FragmentActivity{ 

    private GoogleMap map; 
    private Location location; 
    private ArrayList<LatLng> mLocationsLatLngList; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.map_screen); 


     map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap(); 
     CameraPosition cameraPosition = new CameraPosition.Builder() 
     .target(Your_CurrentLocation).zoom(13).bearing(0) // Sets the orientation of the camera to east 
     .tilt(30) // Sets the tilt of the camera to 30 degrees 
     .build(); // Creates a CameraPosition from the builder 
     map.setBuildingsEnabled(true); 
     map.setMyLocationEnabled(true); 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

    } 
} 

XML -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <fragment 
     android:id="@+id/mapfragment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     class="com.google.android.gms.maps.SupportMapFragment" /> 

</LinearLayout> 
+0

看看打開地圖的活動不在FragmentActivity http://www.learn2crack.com/2013/12/android-google-maps-api-v2-example.html – 2014-10-07 05:58:42

+1

嘿1 suggasion如果你提高你的聲譽在stackppverflow不答案所有的問題都只是回答你賺得更多的寶貴答案,如果你真的幫助人,而不是在評論中提供這種類型的答案。不作爲一個挑釁的答案 – 2014-10-07 05:59:00

+0

你知道什麼時候使用'SupportMapFragment'和'MapFragment'嗎?[官方文檔](https://developers.google.com/maps/documentation/android/) – kId 2014-10-07 06:19:44

0

您正在使用SupportMapFragmentXML,所以你必須使用getSupportFragmentManager代替getFragmentManager

使用此代碼來獲得FragmentMap

SupportMapFragment fr = (SupportMapFragment)this.getSupportFragmentManager().findFragmentById(R.id.mapfragment); 
GoogleMap Map = fr.getMap(); 

Activity必須擴展FragmentActivity