Java 从9.0.1升级到google play services 9.4.0后,在.getMap()上出现错误;
从9.0.1升级到google play services 9.4.0后,在.getMap()上出现错误;如果我做谷歌播放服务9.0.1回来。我的应用程序在启动时崩溃…请大家帮帮我…希望我现在可以做…提前Java 从9.0.1升级到google play services 9.4.0后,在.getMap()上出现错误;,java,android,google-maps,google-maps-api-3,Java,Android,Google Maps,Google Maps Api 3,从9.0.1升级到google play services 9.4.0后,在.getMap()上出现错误;如果我做谷歌播放服务9.0.1回来。我的应用程序在启动时崩溃…请大家帮帮我…希望我现在可以做…提前 public class MapsActivity extends FragmentActivity { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { su
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mMap = mapFragment.getMap();
if (mMap != null) {
new MarkerTask().execute();
}
}
}
private class MarkerTask extends AsyncTask<Void, Void, String> {
private static final String LOG_TAG = "ExampleApp";
private static final String SERVICE_URL = "http://www.xxxxxxx.com/bar/mob/map.php?city=&location=";
// 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);
System.out.println("Returned value " + jsonArray.toString());
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();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
// Create a marker for each city in the JSON data.
mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.title(jsonObj.getString("barname"))
.snippet(jsonObj.getString("address"))
.position(latLng));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
}}
公共类映射活动扩展了FragmentActivity{
私有谷歌地图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
setupmapifneed();
}
私有void setUpMapIfNeeded(){
如果(mMap==null){
MapFragment MapFragment=(MapFragment)getFragmentManager()
.findFragmentById(R.id.map);
mMap=mapFragment.getMap();
如果(mMap!=null){
新建MarkerTask().execute();
}
}
}
私有类MarkerTask扩展了AsyncTask{
私有静态最终字符串日志\u TAG=“ExampleApp”;
私有静态最终字符串服务\u URL=”http://www.xxxxxxx.com/bar/mob/map.php?city=&location=";
//由此对象的execute()方法调用
@凌驾
受保护字符串doInBackground(无效…参数){
HttpURLConnection conn=null;
final StringBuilder json=新StringBuilder();
试一试{
//连接到web服务
URL=新URL(服务URL);
conn=(HttpURLConnection)url.openConnection();
InputStreamReader in=新的InputStreamReader(conn.getInputStream());
//将JSON数据读入StringBuilder
int-read;
char[]buff=新字符[1024];
while((read=in.read(buff))!=-1){
append(buff,0,read);
}
}捕获(IOE异常){
Log.e(日志标签,“连接到服务时出错”,e);
//抛出新IOException(“连接到服务时出错”,e);//未捕获
}最后{
如果(conn!=null){
连接断开();
}
}
返回json.toString();
}
//在完全执行doInBackground()方法后执行
@凌驾
受保护的void onPostExecute(字符串json){
试一试{
//将JSON字符串反序列化为城市对象数组
JSONArray JSONArray=新JSONArray(json);
System.out.println(“返回值”+jsonArray.toString());
for(int i=0;i
MapFragment.getMap()自2014年12月起就被弃用,随着2016年6月27日的新版本,getMap()已被删除。现在,好的(也是唯一的)方法是使用getMapAsync()
有关更多详细信息,请参阅:
以前不推荐使用的getMap()函数在Google Play services SDK中不再可用。(它仍然可以在Android设备上提供的Google Play services APK中使用。)getMap()函数自2014年12月以来一直被弃用。有关从getMap()转换为getMapAsync()的帮助,请参阅发布日志
你可以看一下
如果您在从getMap()切换到getMapAsync()时遇到问题,您还可以查看答案。查看此答案以获取更多帮助可能的重复答案可以节省我的时间。非常感谢。