所以我试图在Json文件中显示我的地图上的标记并且它们没有显示我已将它缩小到该行
map = mapFragment.getMapAsync(this);
它给了我错误
不兼容的类型: 必填:com.google.android.gms.maps.GoogleMap Found:Void
以下是代码的其余部分:
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String LOG_TAG = "ExampleApp";
private static final String SERVICE_URL = "https://api.myjson.com/bins/4jb09";
protected GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (map == null) {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
map = mapFragment.getMapAsync(this);
if (map != null) {
//setUpMap();
new MarkerTask().execute();
}
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
setUpMap();
}
private void setUpMap() {
// Retrieve the city data from the web service
// In a worker thread since it's a network operation.
new Thread(new Runnable() {
public void run() {
try {
retrieveAndAddCities();
} catch (IOException e) {
Log.e(LOG_TAG, "Cannot retrive cities", e);
return;
}
}
}).start();
}
protected void retrieveAndAddCities() throws IOException {
HttpURLConnection conn = null;
final StringBuilder json = new StringBuilder();
try {
// Connect to the web service
URL url = new URL(SERVICE_URL);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Read the JSON data into the StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
json.append(buff, 0, read);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to service", e);
throw new IOException("Error connecting to service", e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
// Create markers for the city data.
// Must run this on the UI thread since it's a UI operation.
runOnUiThread(new Runnable() {
public void run() {
try {
createMarkersFromJson(json.toString());
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
});
}
void createMarkersFromJson(String json) throws JSONException {
// De-serialize the JSON string into an array of city objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
// Create a marker for each city in the JSON data.
JSONObject jsonObj = jsonArray.getJSONObject(i);
map.addMarker(new MarkerOptions()
.title(jsonObj.getString("name"))
.snippet(Integer.toString(jsonObj.getInt("population")))
.position(new LatLng(
jsonObj.getJSONArray("latlng").getDouble(0),
jsonObj.getJSONArray("latlng").getDouble(1)
))
);
}
}
private class MarkerTask extends AsyncTask<Void, Void, String> {
private static final String LOG_TAG = "ExampleApp";
private static final String SERVICE_URL = "https://api.myjson.com/bins/4jb09";
// Invoked by execute() method of this object
@Override
protected String doInBackground(Void... args) {
HttpURLConnection conn = null;
final StringBuilder json = new StringBuilder();
try {
// Connect to the web service
URL url = new URL(SERVICE_URL);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Read the JSON data into the StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
json.append(buff, 0, read);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to service", e);
//throw new IOException("Error connecting to service", e); //uncaught
} finally {
if (conn != null) {
conn.disconnect();
}
}
return json.toString();
}
// Executed after the complete execution of doInBackground() method
@Override
protected void onPostExecute(String json) {
try {
// De-serialize the JSON string into an array of city objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
LatLng latLng = new LatLng(jsonObj.getJSONArray("latlng").getDouble(0),
jsonObj.getJSONArray("latlng").getDouble(1));
//move CameraPosition on first result
if (i == 0) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng).zoom(13).build();
map.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
// Create a marker for each city in the JSON data.
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.title(jsonObj.getString("name"))
.snippet(Integer.toString(jsonObj.getInt("population")))
.position(latLng));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
}
}
答案 0 :(得分:0)
mapFragment.getMapAsync(本);返回类型无效 将 setUpMapIfNeeded()更改为以下
mn.vals <- read.table(input_2)
out.vals <- vector(length=50)
for(i in 1:50){
input_1 <- read.csv(file=paste0("file", i, ".csv"), header=TRUE, as.is=TRUE)
out.vals[i] <- <some function taking file=input_1, mean=mn.vals[i]>
}
out.vals
标记设置是否在onMapReady()中工作。
答案 1 :(得分:0)
使用SupportMapFragment而不是MapFragment。您可以从以下代码示例中获取帮助。
将MapFragment定义为:
private SupportMapFragment supportMapFragment;
GoogleMap mGoogleMap; //for google map
在您的活动创建中,您可以这样做:
if(supportMapFragment==null){
supportMapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
//do load your map and other map stuffs here...
}
});
希望这会对你有所帮助......
答案 2 :(得分:0)
您的代码返回此错误Incompatible types: Required: com.google.android.gms.maps.GoogleMap Found: Void
因为getMapAsync()
没有返回任何内容。如果您查看此功能的文档:
public void getMapAsync (OnMapReadyCallback callback)
设置一个回调对象,该对象将在准备好使用GoogleMap实例时触发。
注意:强>
必须从主线程调用此方法。回调将在主线程中执行。如果用户的设备上未安装Google Play服务,则在用户安装回调之前不会触发回调。在创建后立即销毁GoogleMap的极少数情况下,不会触发回调。回调提供的GoogleMap对象为非null。 OnMapReadyCallback是一个需要实现并通过此函数传递的接口。目前没有任何内容分配给您的googleMap变量,您应该在实现 OnMapReadyCallback
的此代码块中设置其值@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
}
答案 3 :(得分:0)
将其更改为
map.getMapAsync(this);
而不是
map = mapFragment.getMapAsync(this);
因为
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
}
此方法的返回类型为void,因此不会返回任何内容。 你必须这样做:
map.getMapAsync(this)