Android,无法解析getMap()方法 [英] Android, cannot resolve method getMap()

查看:137
本文介绍了Android,无法解析getMap()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这段代码有问题,无法解析方法getMap()。
我找不到问题在哪里,请帮忙用代码,我使用Android Studio并需要将json的标记位置与Json 。

  googleMap =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.googleMap))。getMap();` 

这里是完整的代码

  public class MainActivity extends FragmentActivity {

private GoogleMap googleMap;
private Double Latitude = 0.00;
private Double Longitude = 0.00;
私人GoogleApiClient客户端2;

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if(Build.VERSION.SDK_INT> 9){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build();
StrictMode.setThreadPolicy(政策);
}

ArrayList< HashMap< String,String>> location = null;
String url =http://www.evil.cl/getLatLon.php;
尝试{

JSONArray data = new JSONArray(getHttpGet(url));

location = new ArrayList< HashMap< String,String>>();
HashMap< String,String>地图;

for(int i = 0; i< data.length(); i ++){
JSONObject c = data.getJSONObject(i);

map = new HashMap< String,String>();
map.put(LocationID,c.getString(LocationID));
map.put(Latitude,c.getString(Latitude));
map.put(Longitude,c.getString(Longitude));
map.put(LocationName,c.getString(LocationName));
location.add(map);


$ b catch(JSONException e){
e.printStackTrace(); $($ Support

$ b googleMap =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.googleMap))。getMap();

Latitude = Double.parseDouble(location.get(0).get(Latitude)。toString());
Longitude = Double.parseDouble(location.get(0).get(Longitude)。toString());
LatLng坐标=新LatLng(纬度,经度);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate,17));

for(int i = 0; i< location.size(); i ++){
Latitude = Double.parseDouble(location.get(i).get(Latitude))的ToString());
Longitude = Double.parseDouble(location.get(i).get(Longitude)。toString());
String name = location.get(i).get(LocationName)。toString();
MarkerOptions marker = new MarkerOptions()。position(new LatLng(Latitude,Longitude))。title(name);
googleMap.addMarker(marker);
}

client2 = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();


public static String getHttpGet(String url){
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
尝试{
HttpResponse响应= client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200){
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
字符串行; ((line = reader.readLine())!= null){
str.append(line);
while
}
} else {
Log.e(Log,Failed to download result ..);
}
} catch(ClientProtocolException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
return str.toString();
}

@Override
public void onStart(){
super.onStart();
client2.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW,
Main Page,


Uri.parse(http:// host / path),

Uri.parse(android-app://com.example.hector.prueba1/http/host/path)
);
AppIndex.AppIndexApi.start(client2,viewAction);
}

@Override
public void onStop(){
super.onStop();
Action viewAction = Action.newAction(
Action.TYPE_VIEW,
Main Page,
Uri.parse(http:// host / path),
Uri.parse(android-app://com.example.hector.prueba1/http/host/path)
);
AppIndex.AppIndexApi.end(client2,viewAction);
client2.disconnect();


$ / code>


解决方案

getMap ()方法已折旧,但现在在play-services 9.2更新后,它已被删除,所以最好使用getMapAsync()。只有当你不愿意为你的应用程序更新play-services到9.2时,你仍然可以使用getMap()。为了使用getMapAsync(),实现OnMapReadyCallback接口您的活动或片段:

  public class Fragment_Map extends android.support.v4.app.Fragment 
implements OnMapReadyCallback {然后,在初始化映射的同时,使用getMapAsync()而不是getMap():

>

  private void initializeMap(){
if(mMap == null){
SupportMapFragment mapFrag =(SupportMapFragment)getChildFragmentManager ().findFragmentById(R.id.fragment_map);
mapFrag.getMapAsync(this);
}
}

@Override
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;
setUpMap();
}


I have problem with this code, "cannot resolve method getMap(). I dont find where is the problem, please need help with code, I use Android Studio and need to take markers locations of mySQL with Json.

    googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();`

And here is full code

 public class MainActivity extends FragmentActivity {

    private GoogleMap googleMap;
    private Double Latitude = 0.00;
    private Double Longitude = 0.00;
    private GoogleApiClient client2;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        ArrayList<HashMap<String, String>> location = null;
        String url = "http://www.evil.cl/getLatLon.php";
        try {

            JSONArray data = new JSONArray(getHttpGet(url));

            location = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> map;

            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);

                map = new HashMap<String, String>();
                map.put("LocationID", c.getString("LocationID"));
                map.put("Latitude", c.getString("Latitude"));
                map.put("Longitude", c.getString("Longitude"));
                map.put("LocationName", c.getString("LocationName"));
                location.add(map);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();

        Latitude = Double.parseDouble(location.get(0).get("Latitude").toString());
        Longitude = Double.parseDouble(location.get(0).get("Longitude").toString());
        LatLng coordinate = new LatLng(Latitude, Longitude);
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17));

        for (int i = 0; i < location.size(); i++) {
            Latitude = Double.parseDouble(location.get(i).get("Latitude").toString());
            Longitude = Double.parseDouble(location.get(i).get("Longitude").toString());
            String name = location.get(i).get("LocationName").toString();
            MarkerOptions marker = new MarkerOptions().position(new LatLng(Latitude, Longitude)).title(name);
            googleMap.addMarker(marker);
        }

        client2 = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    public static String getHttpGet(String url) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
            } else {
                Log.e("Log", "Failed to download result..");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();
    }

    @Override
    public void onStart() {
        super.onStart();
        client2.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW,
                "Main Page", 


                Uri.parse("http://host/path"),

                Uri.parse("android-app://com.example.hector.prueba1/http/host/path")
        );
        AppIndex.AppIndexApi.start(client2, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW,
                "Main Page", 
                Uri.parse("http://host/path"),
                Uri.parse("android-app://com.example.hector.prueba1/http/host/path")
        );
        AppIndex.AppIndexApi.end(client2, viewAction);
        client2.disconnect();
    }
}

解决方案

getMap() method was depreciated but now after the play-services 9.2 update, it has been removed, so better use getMapAsync(). You can still use getMap() only if you are not willing to update the play-services to 9.2 for your app.

To use getMapAsync(), implement the OnMapReadyCallback interface on your activity or fragment:

public class Fragment_Map extends android.support.v4.app.Fragment
      implements OnMapReadyCallback { }

Then, while initializing the map, use getMapAsync() instead of getMap():

 private void initializeMap() {
  if (mMap == null) {
    SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map);
    mapFrag.getMapAsync(this);
  }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();
}

这篇关于Android,无法解析getMap()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆