我是使用 Google Maps API 在 android 中进行开发的新手。我已经能够设置 map 并测试基本功能,但是我无法将文档中显示的逻辑实现到我自己的代码中。
我通过 google 的文档进行了研究并发现,您必须使用 map 检查交通数据是否可用:
public final boolean isTrafficEnabled()
然后调用方法:
public final boolean isTrafficEnabled() {
return mMap.isTrafficEnabled();
}
public final void setTrafficEnabled(boolean enabled) {
mMap.setTrafficEnabled(enabled);
}
我不完全确定如何实现这一点,因为我完全是开发新手。我在另一个文档来源中找到了以下示例:
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
但我似乎无法弄清楚如何正确地做到这一点。我是否必须以任何方式编辑 list XML,或者这一切都是从 mainActivity 完成的?这是我的完整 Activity 代码:
package example.testdevice;
import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) { //checks if APK is available; if it is, display Map
setContentView(R.layout.activity_map);
if (initMap()){
Toast.makeText(this, "Ready to Map", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); //pass this as context
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST); //error code, activity, request code
dialog.show();
}
else {
Toast.makeText(this, "Can't connect to Google Play Services", Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFrag =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); // reference to support map fragment
mMap = mapFrag.getMap();
}
return (mMap != null);
}
public final boolean isTrafficEnabled() {
return mMap.isTrafficEnabled();
}
public final void setTrafficEnabled(boolean enabled) {
mMap.setTrafficEnabled(enabled);
}
}
map 已加载,但未显示任何类型的交通。任何和所有的帮助将不胜感激;提前谢谢你。
最佳答案
为了能够显示流量数据,您应该考虑以下问题,
确保在 Google map 中检测到您的当前位置
确保您的 Google map 有您当前位置的路况数据。
您也可以尝试以下代码。它会正确初始化 map ,然后在检测到您的当前位置后设置交通数据。
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
//load the traffic now
googleMap.setTrafficEnabled(true);
}
});
}
}
}
关于android - 如何包含谷歌地图的交通图层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31657822/