gpt4 book ai didi

java - 放置在 fragment 中时不显示此处的 map

转载 作者:行者123 更新时间:2023-12-02 09:47:33 26 4
gpt4 key购买 nike

我正在尝试在我的应用程序中使用 HERE map 。然而,当我尝试在 fragment View 中查看 map 时,我似乎遇到了一些困难。本地图显示在我的主要 Activity 中时,没有出现此问题。

我关注these举例作为基础。在 Activity 中使用时效果很好。

我查看过this线程无济于事。

您可能会找到我的代码的简化版本,该代码应该查看下面的 map :

主要 Activity 类别:


package com.example.heremapapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.here.android.mpa.common.ApplicationContext;

public class MainActivity extends AppCompatActivity {

MapFragmentView mapFragmentView;


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

if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}

initiateMap();
}

public void initiateMap() {
//Start map
mapFragmentView = new MapFragmentView();
mapFragmentView.setM_activity(this);
ApplicationContext applicationContext = new ApplicationContext(getApplicationContext());
mapFragmentView.initMapFragment(applicationContext, this);
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayoutMapView, mapFragmentView).commit();

}
}

MapFragmentView fragment :


package com.example.heremapapplication;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.here.android.mpa.common.ApplicationContext;
import com.here.android.mpa.common.GeoCoordinate;
import com.here.android.mpa.common.MapEngine;
import com.here.android.mpa.common.OnEngineInitListener;
import com.here.android.mpa.common.PositioningManager;
import com.here.android.mpa.mapping.Map;
import com.here.android.mpa.mapping.MapView;
import java.io.File;


public class MapFragmentView extends Fragment {

private AppCompatActivity m_activity;
private PositioningManager positioningManager;
private MapEngine mapEngine = MapEngine.getInstance();
private MapView mapView;
private static View v;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (v != null) {
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null)
parent.removeView(v);

}
try {
v = inflater.inflate(R.layout.map_fragment, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return v;

}


public MapFragmentView() {
}

private Map map;

public void initMapFragment(ApplicationContext applicationContext, Context context) {
/* Locate the mapFragment UI element */
mapView = new MapView(context);

// Set path of isolated disk cache
String diskCacheRoot = Environment.getExternalStorageDirectory().getPath()
+ File.separator + ".isolated-here-maps";
// Retrieve intent name from manifest
String intentName = "";
try {
ApplicationInfo ai = m_activity.getPackageManager().getApplicationInfo(m_activity.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
intentName = bundle.getString("INTENT_NAME");
} catch (PackageManager.NameNotFoundException e) {
Log.e(this.getClass().toString(), "Failed to find intent name, NameNotFound: " + e.getMessage());
}
boolean success = com.here.android.mpa.common.MapSettings.setIsolatedDiskCacheRootPath(diskCacheRoot, intentName);
if (!success) {
// Setting the isolated disk cache was not successful, please check if the path is valid and
// ensure that it does not match the default location
// (getExternalStorageDirectory()/.here-maps).
// Also, ensure the provided intent name does not match the default intent name.
} else {

if (mapView != null) {
System.out.println("hop buraya girildi!");

/* Initialize the SupportMapFragment, results will be given via the called back. m_mapFragment.init(context,new OnEngineInitListener() */
mapEngine.init(applicationContext, new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == Error.NONE) {
positioningManager = PositioningManager.getInstance();
positioningManager.start(PositioningManager.LocationMethod.GPS_NETWORK_INDOOR);
map = mapView.getMap();
GeoCoordinate myCurrentPosition = positioningManager.getPosition().getCoordinate();
} else {
Toast.makeText(m_activity,
"ERROR: Cannot initialize Map with error " + error,
Toast.LENGTH_LONG).show();
}
}
});
}
}
}

public void setM_activity(AppCompatActivity m_activity) {
this.m_activity = m_activity;
}


}

activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/frameLayoutMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:layout_constraintTop_toTopOf="parent">

</FrameLayout>
</RelativeLayout>

map_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapFragment"
android:name="com.here.android.mpa.mapping.MapFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

运行程序后,我们应该能够在屏幕上查看 HEREmap。然而它没有出现。我们看到一个白色的屏幕。如果我们要在后台发送应用程序,屏幕就会变黑。

非常感谢!

最佳答案

进一步查看 MD 的 response 后,我终于成功地在 fragment View 中查看了 map 。

您可以在下面找到我的解决方案:

主要 Activity :

package com.example.heremapapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.here.android.mpa.common.ApplicationContext;

public class MainActivity extends AppCompatActivity {

MapFragmentView mapFragmentView;


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

if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}

initiateMap();
}

public void initiateMap() {
//Start map
mapFragmentView=new MapFragmentView();
getSupportFragmentManager().beginTransaction().replace(R.id.frameLayoutMapView, mapFragmentView).commit();
}
}

MapFragmentView fragment :

/*
* Copyright (c) 2011-2019 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.heremapapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.here.android.mpa.common.ApplicationContext;
import com.here.android.mpa.common.GeoCoordinate;
import com.here.android.mpa.common.MapEngine;
import com.here.android.mpa.common.OnEngineInitListener;
import com.here.android.mpa.common.PositioningManager;
import com.here.android.mpa.mapping.Map;
import com.here.android.mpa.mapping.MapFragment;
import com.here.android.mpa.mapping.MapView;
import java.io.File;


/**
* This class encapsulates the properties and functionality of the Map view.It also implements 4
* types of discovery requests that HERE Android SDK provides as example.
*/
public class MapFragmentView extends Fragment {

private Activity m_activity;
private PositioningManager positioningManager;
private MapEngine mapEngine = MapEngine.getInstance();
private MapView mapView;
private static View v;
private MapFragment mapFragmentView;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("onCreateView Çağrıldı");

v = inflater.inflate(R.layout.map_frame, container, false);

mapFragmentView = new MapFragment();

FragmentManager fm = m_activity.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.simpleFrameLayout,mapFragmentView).commit();

//Here cinsinden ApplicationContext yarattık aşağıda:
com.here.android.mpa.common.ApplicationContext newContext =new ApplicationContext(m_activity);

//O yarttığımız contexti MapFragmentView.init methodu için kullandık (Mine de vardı ;) ):
mapFragmentView.init(newContext, new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(Error error) {
if (error == Error.NONE) {
map = mapFragmentView.getMap();
} else {
Toast.makeText(m_activity,
"ERROR: Cannot initialize Map with error " + error,
Toast.LENGTH_LONG).show();
}
}
});

return v;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(m_activity);
System.out.println("ONETEŞ");
try {
m_activity = (AppCompatActivity) activity;
//mCallbacks = (MyNavigationDrawerCallbacks) activity;
} catch (ClassCastException e) {
System.out.println("ONETEŞ SIKINTI");
}
}


public MapFragmentView() {
}



private Map map;

public void initMapFragment() {
Context context =getContext();
ApplicationContext appContext = new ApplicationContext(context);
/* Locate the mapFragment UI element */
mapView = new MapView(context);
System.out.println("Application context bu: "+appContext.getClass().toString());


// Set path of isolated disk cache
String diskCacheRoot = Environment.getExternalStorageDirectory().getPath()
+ File.separator + ".isolated-here-maps";
// Retrieve intent name from manifest
String intentName = "";
try {
ApplicationInfo ai = m_activity.getPackageManager().getApplicationInfo(m_activity.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
intentName = bundle.getString("INTENT_NAME");
} catch (PackageManager.NameNotFoundException e) {
Log.e(this.getClass().toString(), "Failed to find intent name, NameNotFound: " + e.getMessage());
}
boolean success = com.here.android.mpa.common.MapSettings.setIsolatedDiskCacheRootPath(diskCacheRoot, intentName);
if (!success) {
// Setting the isolated disk cache was not successful, please check if the path is valid and
// ensure that it does not match the default location
// (getExternalStorageDirectory()/.here-maps).
// Also, ensure the provided intent name does not match the default intent name.
} else {

if (mapView != null) {



/* Initialize the SupportMapFragment, results will be given via the called back. m_mapFragment.init(context,new OnEngineInitListener() */
mapEngine.init(appContext, new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == Error.NONE) {
positioningManager = PositioningManager.getInstance();
positioningManager.start(PositioningManager.LocationMethod.GPS_NETWORK_INDOOR);
map = mapView.getMap();
GeoCoordinate myCurrentPosition = positioningManager.getPosition().getCoordinate();
} else {
Toast.makeText(m_activity,
"ERROR: Cannot initialize Map with error " + error,
Toast.LENGTH_LONG).show();
}
}
});
}
}
}

public void setM_activity(AppCompatActivity m_activity) {
this.m_activity = m_activity;
}


}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/frameLayoutMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:layout_constraintTop_toTopOf="parent">

</FrameLayout>
</RelativeLayout>

ma​​p_frame.xml

我们已将 map fragment 转换为框架

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayoutForMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />

关于java - 放置在 fragment 中时不显示此处的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56539090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com