Google地图在活动的中间区域 [英] Google Maps in middle area of an Activity

查看:84
本文介绍了Google地图在活动的中间区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Studio 2.1.2。我已经检查过,大部分问题要么使用旧版本的Android工作室,要么一些旧的类不适用于我的情况。



从文件>新建项目>我使用了Google地图活动选项。我没有改变其中的任何默认代码。它的XML有一个片段。现在有什么方法可以通过片段将这项活动引入另一项活动中?基本上我正在构建一个应用程序,在屏幕的中间部分显示地图。我希望这个中间部分是片段。底部和顶部区域有供用户进行交互的一些组件。



我不想让地图出现在新的活动中,而是停留在特定活动的中间部分。

或者我只是将新的UI组件添加到默认的Google Maps活动中。



起初我有一个创建Fragment的想法.java文件,XML布局),然后将地图代码放在那里,并将其转移到我想要的活动。什么才是最好的方法来完成这件事?我欣赏任何帮助。

解决方案

我已经意识到,无论哪种方式都可以得到相同的结果,以及其他UI组件。无论是使用Google地图活动还是创建空行为并在其中实施地图代码。我的代码包含在下面。我选择了空白活动(Android 2.1.2),然后将地图代码放入.java文件,并将带有标签的地图片段放入活动的XML布局中。



错误在于我曾使用过

  android:name =com.google.android.gms.maps.MapFragment在XML布局中

,但声明并初始化带有SupportMapFragment的mapFragment(对象实例)。从Google文档复制并粘贴代码时,混淆就出现了。当我试图在这里更新我的帖子时,我发现了错误。正确的做法是,如果您使用的是MapFragment(API等级12以上),则全部使用它。如果您决定使用SupportMapFragment(API级别12以下),请使用SupportMapFragment。



下面的代码是更正。



XML布局(activity_main.xml)

 <?xml version =1.0encoding =utf-8?> 
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/tools
android:orientation =vertical
android:layout_width =match_parent
android:layout_height =match_parent
android:paddingBottom =@ dimen / activity_vertical_margin
android:paddingLeft =@ dimen / activity_vertical_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_margin
android:paddingTop =@ dimen / activity_vertical_margin
tools:context =com.example .myapp.MainActivity>

< ScrollView
android:layout_width =match_parent
android:layout_height =match_parent>
< LinearLayout
android:orientation =vertical
android:layout_width =match_parent
android:layout_height =match_parent>

$ b $< LinearLayout
android:layout_width =match_parent
android:layout_height =70dp
android:orientation =horizo​​ntal>

.java文件(MainActivity.java)

 包名称; 

import com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;

$ b public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
$ b @Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate( savedInstanceState);
setContentView(R.layout.activity_main);

SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);


$ b @Override
public void onMapReady(GoogleMap map){
map.addMarker(new MarkerOptions()
.position( new LatLng(0,0))
.title(Marker));
}

}


I am working with Android studio 2.1.2 . I have checked around and most of the questions either use older versions of Android studio and some old classes that don't apply to my situation.

From file > new Project > I used option Google Maps Activity. I have not changed any of the default code that are in it. Its XML has a Fragment. Now is there a way to bring in this activity as it is into another Activity by means of a Fragment? Basically I am building an app that shows a map in the middle portion of the screen. This middle portion I hope would be a Fragment. The bottom and top area has some components for the user to interact.

I am not trying to make the map come up in a new Activity but rather remain in the middle portion of a particualar Activity.

Or do I just add new UI components to the default Google Maps Activity.

At first I had an idea of creating a Fragment(with its own .java file, XML Layout)then placing the map code in there and taking it over to the Activity where I want it. What would be the best way to get this done? I appreciate any help.

解决方案

I have realised that either way one can arrive at the same result, a map inside an Activity, alongside other UI Components. Whether Google Maps Activity is used or if an Empty Activity is created and map code implemented in it. My code is included below. I chose an Empty Activity (Android 2.1.2) and then brought in the map code into the .java file and placed the map fragment with tag in the XML Layout of the activity.

The error was that I had used

android:name="com.google.android.gms.maps.MapFragment"

in the XML Layout but declared and initialised mapFragment(the object instance) with SupportMapFragment. The mix up came while copying and pasting code from the Google Documentation. As I was trying to update my post here I spotted the error. The correct thing to do is if you are using MapFragment(API level 12 and over) use it all through. If you decide to use SupportMapFragment(below API level 12) use SupportMapFragment all through.

The code below is the correction. Just in case anyone has the same issue.

XML Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myapp.MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/teachers_link"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/parent_link"
            android:layout_marginLeft="20dp" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="340dp"
        android:orientation="vertical">
        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="135dp"
        android:layout_marginTop="12dp"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/search_term" />
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:id="@+id/select_level"
            android:entries="@array/select_level"></Spinner>
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/select_course"
            android:entries="@array/select_course"></Spinner>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_search" />
    </LinearLayout>


</LinearLayout>

</ScrollView>

</LinearLayout>

.java file (MainActivity.java)

package name;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends AppCompatActivity implements OnMapReadyCallback{

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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
}

}

这篇关于Google地图在活动的中间区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆