Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将SupportMapFragment添加到LinearLayout_Android_Google Maps_Android Layout - Fatal编程技术网

Android 将SupportMapFragment添加到LinearLayout

Android 将SupportMapFragment添加到LinearLayout,android,google-maps,android-layout,Android,Google Maps,Android Layout,我需要添加一个使用AndroidStudio创建的SupportMapFragment。使用它的片段活动工作正常。它显示的是西德尼岛上有is标记的地图。 但我需要将该地图放在一个布局(即线性布局)上,以添加按钮并与地图交互 我创建了一个类MapsActivity.java及其相应的布局活动\u maps.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.a

我需要添加一个使用AndroidStudio创建的SupportMapFragment。使用它的片段活动工作正常。它显示的是西德尼岛上有is标记的地图。 但我需要将该地图放在一个布局(即线性布局)上,以添加按钮并与地图交互

我创建了一个类MapsActivity.java及其相应的布局活动\u maps.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some Text"
        android:id="@+id/textView" />


    <fragment xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scrollbars="vertical"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>
(二)

(三)


下一步我可以试试什么?

像这样试试。修改第一个

您正在使用创建新的
MapFragment
SupportMapFragment.newInstance()而不是从xml获取
地图碎片


让您的
活动
在MapReadyCallback上实施
。例如

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
onCreate
中,在
setContentView
之后

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
调用
onMapReady
时,您可以使用
googleMap

@Override
public void onMapReady(GoogleMap googleMap) {

GoogleMap
对象准备好供您使用之前可能需要一段时间,因此
getMapAsync
OnMapReadyCallback
我在这里为感兴趣的人发布了完整的答案,我发现它不是那么直观。这使您可以将地图添加到自定义布局中

public class MapsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        FragmentManager myFragmentManager = getSupportFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map);
        GoogleMap mMap = mapFragment.getMap();

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-35, 152);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker near Sidney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

}
布局:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some Text"
        android:id="@+id/textView" />


    <fragment xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scrollbars="vertical"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>

这样可以在屏幕上显示谷歌地图


公共类RouteFinderActivity扩展了AppCompatActivity在MapreadyCallback上的实现{
谷歌地图;
位置;
按钮btn\u搜索\u路线;
EditText et_源,et_目标;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u route\u finder);
btn_搜索_路线=findViewById(R.id.btn_搜索_路线);
et_源=findviewbyd(R.id.et_源);
et_目的地=findViewById(R.id.et_目的地);
btn_search_route.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
String source=et_source.getText().toString();
String destination=et_destination.getText().toString();
if(TextUtils.isEmpty(源)){
et_source.setError(“输入Soruce point”);
}else if(TextUtils.isEmpty(目的地)){
et_destination.setError(“输入目的点”);
}否则{
字符串sendstring=”http://maps.google.com/maps?saddr=" +
来源+
“&daddr=”+
目的地;
意向意向=新意向(android.content.Intent.ACTION\u视图,
parse(sendstring));
星触觉(意向);
}
}
});
((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
地图=谷歌地图;
map.setMapType(1);
map.setMyLocationEnabled(true);
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
标准=新标准();
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)==PackageManager.permission\u已授予
&&ContextCompat.checkSelfPermission(此,Manifest.permission.ACCESS\u位置)==PackageManager.permission\u已授予){
location=locationManager.getLastKnownLocation(locationManager.getBestProvider(条件,false));
如果(位置!=null){
//变焦
CameraPosition CameraPosition=新建CameraPosition.Builder()
.zoom(17)
.target(新的LatLng(location.getLatitude(),location.getLongitude())
.轴承(90)
.倾斜(40)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
}
显示



查看:在哪里可以找到com.google.android.gms.maps.SupportMapFragment的依赖关系?对,我已经更正了,但仍然不起作用:com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)上的mapFragment仍然为Null
原因:java.lang.NullPointerException:尝试调用空对象引用上的虚拟方法“com.google.android.gms.maps.GoogleMap.com.google.android.gms.maps.SupportMapFragment.getMap()”发现另一个错误。方法在
setContentView(R.layout.activity\u映射)之前调用谢谢你的帮助!你能发布一个更新的解决方案吗,getMap()已被弃用。谢谢
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap googleMap) {
public class MapsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        FragmentManager myFragmentManager = getSupportFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map);
        GoogleMap mMap = mapFragment.getMap();

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-35, 152);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker near Sidney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some Text"
        android:id="@+id/textView" />


    <fragment xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scrollbars="vertical"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>