我正在尝试使用OpenWeatherMap.org在Weather应用中实现TileProvider
。问题是它没有在谷歌地图上显示降水。我的{J}代码MapsActivity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private static String OWM_TILE_URL = "http://tile.openweathermap.org/map/%s/%d/%d/%d.png appid=ffe8e3ec39ae79c82c32ebba7059ab14";
private Spinner spinner;
private String tileType = "clouds";
private TileOverlay tileOver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
spinner = findViewById(R.id.tileType);
// String tileName
String[] tileName = new String[]{"Clouds", "Temperature","Precipitations", "Snow", "Rain", "Wind", "Sea level press."};
ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tileName);
spinner.setAdapter(adpt);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Check click
switch (position) {
case 0:
tileType = "clouds";
break;
case 1:
tileType = "temp";
break;
case 2:
tileType = "precipitation";
break;
case 3:
tileType = "snow";
break;
case 4:
tileType = "rain";
break;
case 5:
tileType = "wind";
break;
case 6:
tileType = "pressure";
break;
}
if (mMap != null) {
tileOver.remove();
setUpMap();
}
}
});
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play
services APK is correctly
* installed) and the map has not already been instantiated.. This
will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p/>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a
prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the
prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be `enter code here` * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
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.
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMapAsync(this);
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move
the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
// Add weather tile
//tileOver = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(createTilePovider()));
tileOver = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(createTransparentTileProvider()));
}
private TileProvider createTransparentTileProvider() {
return new TransparentTileOwm(tileType);
}
// tileProvider method
private TileProvider createTilePovider() {
TileProvider tileProvider = new UrlTileProvider(256, 256) {
@Override
public URL getTileUrl(int x, int y, int zoom) {
String fUrl = String.format(OWM_TILE_URL, tileType == null ? "clouds" : tileType, zoom, x, y);
URL url = null;
try {
url = new URL(fUrl);
}
catch(MalformedURLException mfe) {
mfe.printStackTrace();
}
return url;
}
} ;
return tileProvider;
}
@Override
public void onMapReady(GoogleMap googleMap) {
}
}
以及XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MapsActivity" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:id="@+id/tileType"/>
</RelativeLayout>
我已经按照下面的教程来实现tile提供程序 https://www.survivingwithandroid.com/2015/03/android-google-map-add-weather-data-tile-2.html
答案 0 :(得分:0)
您的OWM_TILE_URL
模板:
private static String OWM_TILE_URL = "http://tile.openweathermap.org/map/%s/%d/%d/%d.png appid=ffe8e3ec39ae79c82c32ebba7059ab14";
不正确:您在?
参数之前错过了appid
符号。应该是:
private static String OWM_TILE_URL = "http://tile.openweathermap.org/map/%s/%d/%d/%d.png?appid=ffe8e3ec39ae79c82c32ebba7059ab14";