2016-07-10 30 views
-1

Hallo, ich versuche, eine kml-Datei in meine Android-Apps zu importieren, die Marker für jeden Punkt in der Datei ablegen wird. Ich verfolge die grundlegenden Anweisungen vonJava - kann getMap() nicht lösen google maps api v2

https://developers.google.com/maps/documentation/android-api/utility/setup

Wenn ich versuche,

KmlLayer layer = new KmlLayer(getMap(), R.raw.kmlFile, getApplicationContext()); 

ich den Fehler zu nennen getMap nicht auflösen(). Von dem, was ich finde, sollte es in com.google.android.gms.maps.SupportMapFragment enthalten sein; Aber selbst mit diesem Namen funktioniert es immer noch nicht.

hier ist mein Java

package pcbapps.pokemap; 

import android.net.Uri; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 
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; 


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 
private KmlLayer layer; 
private double lat, lng; 
/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 

@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); 
    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
} 


/** 
* 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; 

    // Add a marker in Sydney and move the camera 
    LatLng sydney = new LatLng(-34, 151); 


    KmlLayer layer; 
    layer = new KmlLayer(getMap(), R.raw.scullin, getApplicationContext()); 
    layer.addLayerToMap(); 
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 

    //mMap.addMarker(new MarkerOptions().) 



} 


@Override 
public void onStart() { 
    super.onStart(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client.connect(); 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Maps Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app URL is correct. 
      Uri.parse("android-app://pcbapps.pokemap/http/host/path") 
    ); 
    AppIndex.AppIndexApi.start(client, viewAction); 
} 

@Override 
public void onStop() { 
    super.onStop(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    Action viewAction = Action.newAction(
      Action.TYPE_VIEW, // TODO: choose an action type. 
      "Maps Page", // TODO: Define a title for the content shown. 
      // TODO: If you have web page content that matches this app activity's content, 
      // make sure this auto-generated web page URL is correct. 
      // Otherwise, set the URL to null. 
      Uri.parse("http://host/path"), 
      // TODO: Make sure this auto-generated app URL is correct. 
      Uri.parse("android-app://pcbapps.pokemap/http/host/path") 
    ); 
    AppIndex.AppIndexApi.end(client, viewAction); 
    client.disconnect(); 
} 
} 

Auch dies ist der richtige Weg, um Informationen aus einer KML-Datei in Android zu importieren. Danke für die Hilfe.

Antwort

1

ändern

layer = new KmlLayer(getMap(), R.raw.scullin, getApplicationContext()); 

für

layer = new KmlLayer(mMap, R.raw.scullin, getApplicationContext()); 
2

Sie müssen getMap() nicht aufrufen, da der Callback 'onMapReady (GoogleMap googleMap)' die GoogleMap-Instanz bereitstellt. So ändern Sie einfach den Anruf an:

layer = new KmlLayer(googleMap, R.raw.scullin, getApplicationContext()); 
Verwandte Themen