11

我最近开始在 Android 上使用 Google Maps API v2 并偶然发现了另一个障碍。我无法弄清楚如何在 API v1 中添加叠加层,以使用 lat 和 long 在某些位置显示标记。我还希望它使用 long 和 lat 在同一个城镇开始。到目前为止,我为地图的起始位置添加了一些代码,但它不起作用。我想我应该用java而不是XML布局来写它,但我不知道如何解决SupportMapFragment来做任何事情。

这是主要活动,我使用的是 SUPPORTMAPFRAGMENT 而不是 MapFragment,并且不想切换。

package com.example.maps;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;

public class Main extends FragmentActivity {

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

    SupportMapFragment fragment = new SupportMapFragment();
    getSupportFragmentManager().beginTransaction()
            .add(android.R.id.content, fragment).commit();
    }
}

这是 XML

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
  android:id="@+id/map"
   android:name="com.google.android.gms.maps.SupportMapFragment"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   map:cameraBearing="112.5"
   map:cameraTargetLat="-33.796923"
   map:cameraTargetLng="150.922433"
   map:cameraTilt="30"
   map:cameraZoom="13"
   map:mapType="normal"
   map:uiCompass="false"
   map:uiRotateGestures="true"
   map:uiScrollGestures="false"
   map:uiTiltGestures="true"
   map:uiZoomControls="false"
   map:uiZoomGestures="true" />

编辑

新的 Java 代码(我排除了导入和包,但它们都在那里。)

public class Main extends FragmentActivity {

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

    SupportMapFragment fragment = new SupportMapFragment();
    getSupportFragmentManager().beginTransaction()
            .add(android.R.id.content, fragment).commit();

    GoogleMap googleMap;
    googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
    LatLng latLng = new LatLng(-33.796923, 150.922433);
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title("My Spot")
            .snippet("This is my spot!")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    googleMap.getUiSettings().setCompassEnabled(true);
    googleMap.getUiSettings().setZoomControlsEnabled(true);
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
}

}

新错误

01-02 01:16:53.333: D/AndroidRuntime(916): Shutting down VM
01-02 01:16:53.333: W/dalvikvm(916): threadid=1: thread exiting with uncaught    exception (group=0x40a70930)
01-02 01:16:53.363: E/AndroidRuntime(916): FATAL EXCEPTION: main
01-02 01:16:53.363: E/AndroidRuntime(916): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maps/com.example.maps.Main}: java.lang.NullPointerException
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.os.Looper.loop(Looper.java:137)
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-02 01:16:53.363: E/AndroidRuntime(916):  at java.lang.reflect.Method.invokeNative(Native Method)
01-02 01:16:53.363: E/AndroidRuntime(916):  at java.lang.reflect.Method.invoke(Method.java:511)
01-02 01:16:53.363: E/AndroidRuntime(916):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-02 01:16:53.363: E/AndroidRuntime(916):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-02 01:16:53.363: E/AndroidRuntime(916):  at dalvik.system.NativeStart.main(Native Method)
01-02 01:16:53.363: E/AndroidRuntime(916): Caused by: java.lang.NullPointerException
01-02 01:16:53.363: E/AndroidRuntime(916):  at com.example.maps.Main.onCreate(Main.java:19)
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.app.Activity.performCreate(Activity.java:5104)
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-02 01:16:53.363: E/AndroidRuntime(916):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-02 01:16:53.363: E/AndroidRuntime(916):  ... 11 more
4

3 回答 3

8

您可以onCreateFragmentActivity.

        GoogleMap googleMap;
        googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
        LatLng latLng = new LatLng(-33.796923, 150.922433);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.addMarker(new MarkerOptions()
                .position(latLng)
                .title("My Spot")
                .snippet("This is my spot!")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        googleMap.getUiSettings().setCompassEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));

端到端解决方案的布局看起来像这样......

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

...这样的活动...

public class NameOfYourActivity extends FragmentActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.nameOfYourLayout);

    GoogleMap googleMap;
    googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
    LatLng latLng = new LatLng(-33.796923, 150.922433);
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title("My Spot")
            .snippet("This is my spot!")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    googleMap.getUiSettings().setCompassEnabled(true);
    googleMap.getUiSettings().setZoomControlsEnabled(true);
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
}
于 2013-01-01T20:55:39.070 回答
2

https://developers.google.com/maps/documentation/android-api/groundoverlay

LatLng NEWARK = new LatLng(40.714086, -74.228697);

GroundOverlayOptions newarkMap = new GroundOverlayOptions()
        .image(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922))
        .position(NEWARK, 8600f, 6500f);
map.addGroundOverlay(newarkMap);
于 2017-03-28T14:10:32.427 回答
1

不确定您是否仍然需要帮助,但这对我有用。

由于您将片段加载到容器中,因此将片段组件加载到内存中需要时间,因此映射为空。为了解决这个问题,我在我的活动的 onAttachedToWindow() 方法中抓取并填充地图,同时仍然在 onCreate 方法中创建片段并将其添加到容器中。这给了片段加载时间,因为在加载窗口时会触发此事件。

SupportMapFragment mMapFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    // Try to obtain the map from the SupportMapFragment.
    mMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag(MAP_FRAG_NAME);

    // Not found so make a new instance and add it to the transaction for swapping
    if (mMapFragment == null) {
        mMapFragment = SupportMapFragment.newInstance();
        ft.add(R.id.fragment_container, mMapFragment, MAP_FRAG_NAME);
    }

    ft.commit();
}

@Override
    public void onAttachedToWindow() {
        // Load the map here such that the fragment has a chance to completely load or else the GoogleMap value may be null
        GoogleMap googleMap;
googleMap = (mMapFragment).getMap();
LatLng latLng = new LatLng(-33.796923, 150.922433);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions()
        .position(latLng)
        .title("My Spot")
        .snippet("This is my spot!")
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));

        super.onAttachedToWindow();
    }

希望这可以帮助。

问候, DMan

于 2013-01-16T13:54:01.057 回答