[英]How to get weather tile layer correctly?
I am trying to get a precipitation weather layer to appear in my Google Maps activity, but cannot understand why it is not showing on the map. 我正在尝试使降水天气图层出现在我的Google地图活动中,但无法理解为什么它没有显示在地图上。 I have implemented my Google Maps Key in app\\src\\debug\\res\\values\\google_maps_api.xml
, have installed Google Play Services in Android SDK manager, and I have set up an account with OpenWeatherMap for their API (which is taken out below). 我已经在app\\src\\debug\\res\\values\\google_maps_api.xml
实现了我的Google Maps Key,并在Android SDK Manager中安装了Google Play服务,并且已经为他们的API设置了OpenWeatherMap帐户(如下所述) )。
The map loads fine, and when I search the layer in a separate browser with inputted zoom,x,y it shows up fine, but I cannot get the layer to appear on my Google Maps activity. 地图加载正常,当我在单独的浏览器中使用输入的zoom,x,y搜索该图层时,它显示正常,但无法使该图层出现在我的Google Maps活动中。
Do I need to define x,y,zoom or are these pulled from the URL? 我需要定义x,y,zoom还是将其从URL中拉出?
Here is my java: 这是我的java:
package com.example.user.project;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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;
import com.google.android.gms.maps.model.TileOverlay;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;
import com.google.android.gms.maps.model.UrlTileProvider;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Move the camera
LatLng indy = new LatLng(39, -86.5);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(indy, 8));
TileProvider tileProvider = new UrlTileProvider(256, 256) {
@Override
public URL getTileUrl(int x, int y, int zoom) {
/* Define the URL pattern for the tile images */
String s = String.format(Locale.US, "http://tile.openweathermap.org/map/precipitation/%d/%d/%d.png?appid={my_key}",
zoom, x, y);
if (!checkTileExists(x, y, zoom)) {
return null;
}
try {
return new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
}
/*
* Check that the tile server supports the requested x, y and zoom.
* Complete this stub according to the tile range you support.
* If you support a limited range of tiles at different zoom levels, then you
* need to define the supported x, y range at each zoom level.
*/
private boolean checkTileExists(int x, int y, int zoom) {
int minZoom = 12;
int maxZoom = 16;
return !(zoom < minZoom || zoom > maxZoom);
}
};
TileOverlay tileOverlay = mMap.addTileOverlay(new TileOverlayOptions()
.tileProvider(tileProvider));
}
}
Here is the Android Studio generated layout: 这是Android Studio生成的布局:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.project.MapsActivity"/>
I have followed tutorials and documentation offered from OpenWeatherMap, Google Maps API, and Android Developers, but do not understand why this is not working. 我遵循了OpenWeatherMap,Google Maps API和Android Developers提供的教程和文档,但是不明白为什么它不起作用。
What am I missing? 我想念什么?
You are setting level 8 as zoom level for your map but you are also limiting zoom levels to be between 12 and 16 for your TileProvider
to be visible. 您将级别8设置为地图的缩放级别,但同时也将缩放级别限制在12到16之间,以使TileProvider
可见。
Remove this: 删除此:
if (!checkTileExists(x, y, zoom)) {
return null;
}
If you don't need any scale restriction you can also remove the checkTileExists
method. 如果不需要任何比例限制,也可以删除checkTileExists
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.