我试图在地图上绘制一些点,但是当我运行我的应用程序时它们没有显示?

时间:2017-08-09 16:08:39

标签: android google-maps mapfragment

这是我用于绘制My Map上的点的Java代码,我已经将其包含在我的Home Fragment XML中,这也是下面的,当我运行应用程序时,Map显示得很好,我也设置了缩放不工作,我的代码中缺少什么。感谢

import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * Created by Aids on 09/08/2017.
 */

public class Map_Code_Home extends Fragment implements OnMapReadyCallback {

    GoogleMap mGoogleMap;
    MapView mMapView;
    View mView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_home, container, false);
        return mView;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mMapView = (MapView) mView.findViewById(R.id.map);
        if(mMapView != null){
            mMapView.onCreate(null);
            mMapView.onResume();
            mMapView.getMapAsync(this);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onMapReady(GoogleMap googleMap) {

        MapsInitializer.initialize(getContext());
        mGoogleMap = googleMap;
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        googleMap.addMarker(new MarkerOptions().position(new LatLng(48.689247, -74.044502)).title("Statue of Liberty"));

        CameraPosition SettingCamera = CameraPosition.builder().target(new LatLng(48.689247, -74.044502)).zoom(40).build();
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(SettingCamera));

    }
}

下面是我在HomeFragment XML布局文件中的Map Fragment。

<fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="fill_parent"
            android:layout_height="275dp"
            android:layout_below="@id/textView3"/>

0 个答案:

没有答案